as.data.table {data.table} | R Documentation |
Functions to check if an object is data.table
, or coerce it if possible.
as.data.table(x, keep.rownames=FALSE, ...) ## S3 method for class 'data.table' as.data.table(x, ...) is.data.table(x)
x |
An R object. |
keep.rownames |
Default is |
... |
Additional arguments to be passed to or from other methods. |
as.data.table
is a generic function with many methods, and other packages can supply further methods.
If a list
is supplied, each element is converted to a column in the data.table
with shorter elements recycled automatically. Similarly, each column of a matrix
is converted separately.
character
objects are not converted to factor
types unlike as.data.frame
.
If a data.frame
is supplied, all classes preceding "data.frame"
are stripped. Similarly, for data.table
as input, all classes preceding "data.table"
are stripped. as.data.table
methods returns a copy of original data. To modify by reference see setDT
and setDF
.
keep.rownames
argument can be used to preserve the (row)names attribute in the resulting data.table
.
data.table
, setDT
, setDF
, copy
, setkey
, J
, SJ
, CJ
, merge.data.table
, :=
, alloc.col
, truelength
, rbindlist
, setNumericRounding
, datatable-optimize
nn = c(a=0.1, b=0.2, c=0.3, d=0.4) as.data.table(nn) as.data.table(nn, keep.rownames=TRUE) as.data.table(nn, keep.rownames="rownames") # char object not converted to factor cc = c(X="a", Y="b", Z="c") as.data.table(cc) as.data.table(cc, keep.rownames=TRUE) as.data.table(cc, keep.rownames="rownames") mm = matrix(1:4, ncol=2, dimnames=list(c("r1", "r2"), c("c1", "c2"))) as.data.table(mm) as.data.table(mm, keep.rownames=TRUE) as.data.table(mm, keep.rownames="rownames") ll = list(a=1:2, b=3:4) as.data.table(ll) as.data.table(ll, keep.rownames=TRUE) as.data.table(ll, keep.rownames="rownames") df = data.frame(x=rep(c("x","y","z"),each=2), y=c(1,3,6), row.names=LETTERS[1:6]) as.data.table(df) as.data.table(df, keep.rownames=TRUE) as.data.table(df, keep.rownames="rownames") dt = data.table(x=rep(c("x","y","z"),each=2), y=c(1:6)) as.data.table(dt)