[ top | up ]

Column Indexes

Syntax

col(x, as.factor=FALSE)

Description

If as.factor is FALSE, col takes a matrix as argument and returns an integer matrix with the same dimensions and whose ij-th element is equal to j. If as.factor is TRUE, col the value is returned as a factor with levels labelled by the column names of x.

Examples

# extract the diagonal of a matrix dx <- x[row(x)==col(x)] # create an identity 5-by-5 matrix x <- matrix(0,nr=5,nc=5) x[row(x)==col(x)] <- 1