as.matrix.Rd
Converts a data.table
into a matrix
, optionally using one
of the columns in the data.table
as the matrix
rownames
.
# S3 method for data.table as.matrix(x, rownames=NULL, rownames.value=NULL, ...)
x | a |
---|---|
rownames | optional, a single column name or column number to use as
the |
rownames.value | optional, a vector of values to be used as the
|
... | Required to be present because the generic `as.matrix` generic has it. Arguments here are not currently used or passed on by this method. |
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.
A new matrix
containing the contents of x
.
data.table
, as.matrix
, data.matrix
array
#> A X Y #> [1,] "a" " 1" "11" #> [2,] "b" " 2" "12" #> [3,] "c" " 3" "13" #> [4,] "d" " 4" "14" #> [5,] "e" " 5" "15" #> [6,] "f" " 6" "16" #> [7,] "g" " 7" "17" #> [8,] "h" " 8" "18" #> [9,] "i" " 9" "19" #> [10,] "j" "10" "20"as.matrix(DT, rownames = "A")#> X Y #> a 1 11 #> b 2 12 #> c 3 13 #> d 4 14 #> e 5 15 #> f 6 16 #> g 7 17 #> h 8 18 #> i 9 19 #> j 10 20as.matrix(DT, rownames = 1)#> X Y #> a 1 11 #> b 2 12 #> c 3 13 #> d 4 14 #> e 5 15 #> f 6 16 #> g 7 17 #> h 8 18 #> i 9 19 #> j 10 20as.matrix(DT, rownames = TRUE)#> X Y #> a 1 11 #> b 2 12 #> c 3 13 #> d 4 14 #> e 5 15 #> f 6 16 #> g 7 17 #> h 8 18 #> i 9 19 #> j 10 20#> X Y #> a 1 11 #> b 2 12 #> c 3 13 #> d 4 14 #> e 5 15 #> f 6 16 #> g 7 17 #> h 8 18 #> i 9 19 #> j 10 20