Description Usage Arguments Details Value See Also Examples
Note that 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.
setnames
operates only on data.frame
s and data.table
s and is used for setting or changing column names by reference.
setattr
is a more general function that allows setting of any attribute to an object by reference.
1 2 |
x |
|
name |
The character attribute name. |
value |
The value to assign to the attribute or |
old |
When |
new |
Optional. New column names, the same length as |
The <- method copies the whole table and we know of no way to avoid that copy without a change in R itself. Please use the set* functions instead, which make no copy at all. That is, please use setattr()
rather than attr(x, name) <- value
, setnames()
rather than names(x) <- value
or colnames(x) <- value
and checkout other set*
functions available for setting keys, reordering rows and columns of data.table
, adding columns by reference. In particular, setattr()
is useful in many situations to set attributes by reference and can be used on any object or part of an object, not just data.table
s.
The input is modified by reference, and returned (invisibly) so it can be used in compound statements; e.g., setnames(DT,"V1", "Y")[, .N, by=Y]
. If you require a copy, take a copy first (using DT2=copy(DT)
). See ?copy
.
Note that setattr
is also in package bit
. Both packages merely expose R's internal setAttrib
function at C level, but differ in return value. bit::setattr
returns NULL
(invisibly) to remind you the function is used for its side effect. data.table::setattr
returns the changed object (invisibly), for use in compound statements.
data.table
, setkey
, setorder
, set
, :=
, setDT
, copy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
DF = data.frame(a=1:2,b=3:4) # base data.frame to demo copies, as of R 2.15.1
try(tracemem(DF)) # try() for R sessions opted out of memory profiling
colnames(DF)[1] <- "A" # 4 copies of entire object
names(DF)[1] <- "A" # 3 copies of entire object
names(DF) <- c("A", "b") # 1 copy of entire object
`names<-`(DF,c("A","b")) # 1 copy of entire object
# What if DF is large, say 10GB in RAM. Copy 10GB, even once, just to change a column name?
DT = data.table(a=1:2,b=3:4,c=5:6)
try(tracemem(DT))
setnames(DT,"b","B") # by name; no match() needed
setnames(DT,3,"C") # by position
setnames(DT,2:3,c("D","E")) # multiple
setnames(DT,c("a","E"),c("A","F")) # multiple by name
setnames(DT,c("X","Y","Z")) # replace all
# And, no copy of DT was made by 'setnames()' at all.
# set attributes - ex: names to a list.
set.seed(1L)
ll <- lapply(1:4, function(x) sample(10))
try(tracemem(DT))
setattr(ll, 'names', letters[1:4])
# once again, no copy of 'll' was made by 'setattr()' at all.
|
[1] "<0x1a84770>"
tracemem[0x1a84770 -> 0x1a83240]:
tracemem[0x1a83240 -> 0x1a83400]:
tracemem[0x1a83400 -> 0x1a834a8]: colnames<-
tracemem[0x1a834a8 -> 0x1a834e0]: colnames<-
tracemem[0x1a834e0 -> 0x1a83518]:
tracemem[0x1a83518 -> 0x1a83588]:
tracemem[0x1a83588 -> 0x1a835c0]:
tracemem[0x1a835c0 -> 0x1a83630]:
tracemem[0x1a83630 -> 0x1a836a0]:
A b
1 1 3
2 2 4
[1] "<0x288b410>"
[1] "<0x288b410>"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.