matrix
Matrix Objects

Description

Creates or tests for matrices. There are different kinds of matrices; all have two dimensions.

Usage

matrix(data = NA, nrow = 1, ncol = 1, byrow = FALSE, dimnames = NULL) 
is.matrix(x) 
as.matrix(x, ...)
## S3 method for class 'data.frame':
as.matrix(x, rownames.force = NA, ...)
## S3 method for class 'POSIXlt':
as.matrix(x, ...)

Arguments

x any object.
data a vector containing the data values for the matrix. Missing values (NAs) are allowed.
nrow the first subscript: the number of rows. The default is ceiling(length(data)/ncol). That is, if ncol is specified, nrow is chosen to match the length of data. If neither nrow nor ncol are specified, then nrow is the length of data and ncol is 1.
ncol the second subscript: the number of columns. If nrow is specified but ncol is not, then ncol is defined as ceiling(length(data)/nrow).
byrow a logical flag. If TRUE, the data values are assumed to be the first row, then the second row, and so on. If FALSE (the default), the values are assumed to be the first column, then the second column, and so on. (The latter is how the data are stored internally.)
dimnames a list of length 2 giving a dimnames attribute for the matrix. Each component must either have length 0 or be a vector of character strings with length equal to the corresponding element of the dim attribute of the result.
rownames.force any object. when rownames.force is a logical value, if TRUE, matrix row names must be generated from the data frame; if FALSE, the rownames attribute is NULL, regardless if the data frame has row names. The default NA or any other values, indicate that if the data frame has no specified row names, the rownames attribute is NULL; but if the data frame has row names, these become the row names of the returned matrix.
... additional arguments to be passed to or from methods.

Details

The as.matrix function is generic. It has a method for data frames and various other classes, and it has a default method.
as.matrix.data.frame is a method for the generic function as.matrix for class data.frame. It can be invoked by calling as.matrix for an object x of the appropriate class, or directly by calling as.matrix.data.frame, regardless of the class of the object.
In as.matrix.data.frame, if element of x is non atomic, and is not recursive, turn elements to vector, and turn vector to list object; if element of x is non numeric, converted element to a character.
Matrix objects are those that have an attribute dim of length 2. The objects can also have an attribute dimnames. If so, dimnames is a list of two character vectors, each of which is either of length zero or gives the labels corresponding to the levels of the corresponding subscript in the matrix.
If mat is a matrix, then as.vector(mat) is a vector containing the data values for mat in normal array order: the first subscript varies most rapidly.
The byrow argument should be set to TRUE if the data values were read from a file arranged by rows.
Value
matrixreturns an nrow by ncol matrix with the same mode as data. If the data does not fill the matrix, it is repeated until the matrix is filled.
is.matrixreturns TRUE if dim(x) is of length 2; otherwise, it returns FALSE.
as.matrixreturns x, if x is a matrix.
  • If x is a vector, it returns a matrix with the data from x and the dimension c(length(x),1).
  • If x is a data frame containing any non-numeric or non-logical columns, it returns a character matrix.
  • If x is a data frame with columns that are all logical, it returns a coerced logical matrix.
  • If x is a data frame with mixed logical and numeric columns, it returns a coerced numeric matrix. (NAs are kept with no change.)
  • If x is a POSIXlt object, it returns a numeric matrix with the following columns: sec, min, hour, mday, mon, year, wday, yday and isdst.
  • if x is either a dist or a noquote object, it returns a matrix.
See Examples below.
as.matrix.data.frameSpotfire Enterprise Runtime for R always returns a matrix with the row names attributes set. and is not compatible with R in this regard, as R returns a matrix with no row names set if the source data frame has "default" row names.
See Also
array, data.frame, dim, dimnames, t, Matrix-product, Arithmetic, matplot, data.matrix, numerical.matrix, as.matrix.dist, POSIXt
Examples
m <- matrix(0, 4, 5) # a 4 by 5 matrix of zeros 
is.matrix(m)  # TRUE

matrix(1:10, 5) # a 5 by 2 matrix matrix(1:10, ncol=2) # the same thing matrix(1:10, ncol=2, byrow=TRUE) # row-major order t(matrix(1:10, nrow=2)) # the same thing

as.matrix(1:3) # as.matrix for a vector df <- data.frame(a = c(1:3), b = c("R", "G", "B"), c = c(TRUE, FALSE, NA)) as.matrix(df, ) # as.matrix for a mixed data frame df1 <- data.frame(a = c(1:3), c = c(TRUE, FALSE,NA))

# as.matrix for a logical-numeric data frame as.matrix(df1, rownames.force = TRUE)

xd <- dist(matrix(1:12, nrow = 3)) as.matrix(xd) # as.matrix for a "dist" object

## POSIXlt plt <- as.POSIXlt(c("1970-01-01 00:00:00", "2038-01-19 03:14:07")) as.matrix(plt)

xn <- noquote(1:12)

as.matrix(xn) # as.matrix for a "noquote" object

Package base version 6.1.1-7
Package Index