data.table (version 1.9.4)

setDT: Convert lists and data.frames to data.table by reference

Description

In 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 set* function data.table provides.

setDT converts lists (both named and unnamed) and data.frames to data.tables by reference. This feature was requested on http://stackoverflow.com/questions/20345022/convert-a-data-frame-to-a-data-table-without-copy{Stackoverflow}.

Usage

setDT(x, giveNames=TRUE, keep.rownames=FALSE)

Arguments

x
A named or unnamed list, data.frame or data.table.
giveNames
For list input to setDT, TRUE automatically generates missing column names. FALSE sets all column names to "".
keep.rownames
For data.frames, TRUE retains the data.frame's row names under a new column rn.

Value

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

Details

When working on large lists or data.frames, it might be both time and memory consuming to convert them to a data.table using as.data.table(.), as this will make a complete copy of the input object before to convert it to a data.table. The 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.

See Also

setkey, setattr, setnames, set, :=, setorder, copy, setDF html{}

Examples

Run this code
set.seed(45L)
X = data.frame(A=sample(3, 10, TRUE), 
         B=sample(letters[1:3], 10, TRUE), 
         C=sample(10), stringsAsFactors=FALSE)

# 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)

Run the code above in your browser using DataLab