as.data.table.RdFunctions 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, ..., key=NULL)
# S3 method for class 'array'
as.data.table(x, keep.rownames=FALSE, key=NULL, sorted=TRUE,
value.name="value", na.rm=TRUE, ...)
is.data.table(x)An R object.
Default is FALSE. If TRUE, adds the input object's names as a separate column named "rn". keep.rownames = "id" names the column "id" instead.
Character vector of one or more column names which is passed to setkeyv.
logical used in array method, default TRUE is overridden when key is provided.
character scalar used in array method, default "value".
logical used in array method, default TRUE will remove rows with NA values.
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.
nn = c(a=0.1, b=0.2, c=0.3, d=0.4)
as.data.table(nn)
#> nn
#> <num>
#> 1: 0.1
#> 2: 0.2
#> 3: 0.3
#> 4: 0.4
as.data.table(nn, keep.rownames=TRUE)
#> rn nn
#> <char> <num>
#> 1: a 0.1
#> 2: b 0.2
#> 3: c 0.3
#> 4: d 0.4
as.data.table(nn, keep.rownames="rownames")
#> rownames nn
#> <char> <num>
#> 1: a 0.1
#> 2: b 0.2
#> 3: c 0.3
#> 4: d 0.4
# char object not converted to factor
cc = c(X="a", Y="b", Z="c")
as.data.table(cc)
#> cc
#> <char>
#> 1: a
#> 2: b
#> 3: c
as.data.table(cc, keep.rownames=TRUE)
#> rn cc
#> <char> <char>
#> 1: X a
#> 2: Y b
#> 3: Z c
as.data.table(cc, keep.rownames="rownames")
#> rownames cc
#> <char> <char>
#> 1: X a
#> 2: Y b
#> 3: Z c
mm = matrix(1:4, ncol=2, dimnames=list(c("r1", "r2"), c("c1", "c2")))
as.data.table(mm)
#> c1 c2
#> <int> <int>
#> 1: 1 3
#> 2: 2 4
as.data.table(mm, keep.rownames=TRUE)
#> rn c1 c2
#> <char> <int> <int>
#> 1: r1 1 3
#> 2: r2 2 4
as.data.table(mm, keep.rownames="rownames")
#> rownames c1 c2
#> <char> <int> <int>
#> 1: r1 1 3
#> 2: r2 2 4
as.data.table(mm, key="c1")
#> Key: <c1>
#> c1 c2
#> <int> <int>
#> 1: 1 3
#> 2: 2 4
ll = list(a=1:2, b=3:4)
as.data.table(ll)
#> a b
#> <int> <int>
#> 1: 1 3
#> 2: 2 4
as.data.table(ll, keep.rownames=TRUE)
#> a b
#> <int> <int>
#> 1: 1 3
#> 2: 2 4
as.data.table(ll, keep.rownames="rownames")
#> a b
#> <int> <int>
#> 1: 1 3
#> 2: 2 4
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)
#> x y
#> <char> <num>
#> 1: x 1
#> 2: x 3
#> 3: y 6
#> 4: y 1
#> 5: z 3
#> 6: z 6
as.data.table(DF, keep.rownames=TRUE)
#> rn x y
#> <char> <char> <num>
#> 1: A x 1
#> 2: B x 3
#> 3: C y 6
#> 4: D y 1
#> 5: E z 3
#> 6: F z 6
as.data.table(DF, keep.rownames="rownames")
#> rownames x y
#> <char> <char> <num>
#> 1: A x 1
#> 2: B x 3
#> 3: C y 6
#> 4: D y 1
#> 5: E z 3
#> 6: F z 6
DT = data.table(x=rep(c("x","y","z"),each=2), y=c(1:6))
as.data.table(DT)
#> x y
#> <char> <int>
#> 1: x 1
#> 2: x 2
#> 3: y 3
#> 4: y 4
#> 5: z 5
#> 6: z 6
as.data.table(DT, key='x')
#> Key: <x>
#> x y
#> <char> <int>
#> 1: x 1
#> 2: x 2
#> 3: y 3
#> 4: y 4
#> 5: z 5
#> 6: z 6
ar = rnorm(27)
ar[sample(27, 15)] = NA
dim(ar) = c(3L,3L,3L)
as.data.table(ar)
#> Key: <V1, V2, V3>
#> V1 V2 V3 value
#> <int> <int> <int> <num>
#> 1: 1 2 2 1.8885049
#> 2: 2 1 2 -1.3045435
#> 3: 2 1 3 0.1764886
#> 4: 2 2 1 -0.5220125
#> 5: 2 2 2 -0.0974451
#> 6: 2 2 3 0.1120381
#> 7: 2 3 2 -0.8267890
#> 8: 2 3 3 -0.2792372
#> 9: 3 2 2 -0.9358474
#> 10: 3 2 3 -0.1339970
#> 11: 3 3 1 0.4681544
#> 12: 3 3 2 -1.5123997