data.table (version 1.11.4) Coerce to data.table


Functions to check if an object is data.table, or coerce it if possible.

Usage, keep.rownames=FALSE, …)

# S3 method for data.table, …)

# S3 method for array, keep.rownames=FALSE, sorted=TRUE,"value", na.rm=TRUE, …)



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.


logical used in array method, default TRUE.

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.

Details 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

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. 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.

See Also

data.table, setDT, setDF, copy, setkey, J, SJ, CJ,, :=, alloc.col, truelength, rbindlist, setNumericRounding, datatable-optimize


Run this code
nn = c(a=0.1, b=0.2, c=0.3, d=0.4), keep.rownames=TRUE), keep.rownames="rownames")

# char object not converted to factor
cc = c(X="a", Y="b", Z="c"), keep.rownames=TRUE), keep.rownames="rownames")

mm = matrix(1:4, ncol=2, dimnames=list(c("r1", "r2"), c("c1", "c2"))), keep.rownames=TRUE), keep.rownames="rownames")

ll = list(a=1:2, b=3:4), keep.rownames=TRUE), keep.rownames="rownames")

DF = data.frame(x=rep(c("x","y","z"),each=2), y=c(1,3,6), row.names=LETTERS[1:6]), keep.rownames=TRUE), keep.rownames="rownames")

DT = data.table(x=rep(c("x","y","z"),each=2), y=c(1:6))

ar = rnorm(27)
ar[sample(27, 15)] = NA
dim(ar) = c(3L,3L,3L)
# }

Run the code above in your browser using DataCamp Workspace