data.table parlance, all
set* functions change their input by reference. That is, no copy is made at all, other than temporary working memory, which is as large as one column.. The only other
data.table operator that modifies input by reference is
:=. Check out the
See Also section below for other
setDT converts lists (both named and unnamed) and data.frames to data.tables by reference. This feature was requested on Stackoverflow.
A named or unnamed
Character vector of one or more column names which is passed to
When working on large
data.frames, it might be both time and memory consuming to convert them to a
as.data.table(.), as this will make a complete copy of the input object before to convert it to a
setDT function takes care of this issue by allowing to convert
lists - both named and unnamed lists and
data.frames by reference instead. That is, the input object is modified in place, no copy is being made.
The input is modified by reference, and returned (invisibly) so it can be used in compound statements; e.g.,
setDT(X)[, sum(B), by=A]. If you require a copy, take a copy first (using
DT2 = copy(DT)). See
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
set.seed(45L) X = data.frame(A=sample(3, 10, TRUE), B=sample(letters[1:3], 10, TRUE), C=sample(10), stringsAsFactors=FALSE) # Convert X to data.table by reference and # get the frequency of each "A,B" combination setDT(X)[, .N, by=.(A,B)] # convert list to data.table # autofill names X = list(1:4, letters[1:4]) setDT(X) # don't provide names X = list(a=1:4, letters[1:4]) setDT(X, FALSE) # setkey directly X = list(a = 4:1, b=runif(4)) setDT(X, key="a") # check.names argument X = list(a=1:5, a=6:10) setDT(X, check.names=TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.