as.matrix {data.table} | R Documentation |
Converts a data.table
into a matrix
, optionally using one
of the columns in the data.table
as the matrix
rownames
.
## S3 method for class 'data.table' as.matrix(x, rownames, ...)
x |
a |
rownames |
optional, a single column name or column index to use as
the |
... |
additional arguments to be passed to or from methods. |
as.matrix
is a generic function in base R. It dispatches to
as.matrix.data.table
if its x
argument is a data.table
.
The method for data.table
s will return a character matrix if there
are only atomic columns and any non-(numeric/logical/complex) column,
applying as.vector
to factors and format
to other
non-character columns. Otherwise, the usual coercion hierarchy (logical <
integer < double < complex) will be used, e.g., all-logical data frames
will be coerced to a logical matrix, mixed logical-integer will give an
integer matrix, etc.
An additional argument rownames
is provided for as.matrix.data.table
to facilitate conversions to matrices where the rownames
are stored
in a single column of x
, e.g. the first column after using
dcast.data.table
.
A new matrix
containing the contents of x
.
data.table
, as.matrix
, data.matrix
array
(dt1 <- data.table(A = letters[1:10], X = 1:10, Y = 11:20)) as.matrix(dt1) # character matrix as.matrix(dt1, rownames = "A") as.matrix(dt1, rownames = 1) as.matrix(dt1, rownames = TRUE) (dt1 <- data.table(A = letters[1:10], X = 1:10, Y = 11:20)) setkey(dt1, A) as.matrix(dt1, rownames = TRUE)