# NOT RUN {
DF = data.frame(a=1:2,b=3:4) # base data.frame to demo copies and syntax
try(tracemem(DF)) # try() for R sessions opted out of memory profiling
colnames(DF)[1] <- "A" # 4 shallow copies (R >= 3.1, was 4 deep copies before)
names(DF)[1] <- "A" # 3 shallow copies
names(DF) <- c("A", "b") # 1 shallow copy
`names<-`(DF,c("A","b")) # 1 shallow copy
DT = data.table(a=1:2,b=3:4,c=5:6) # compare to data.table
try(tracemem(DT)) # by reference, no deep or shallow copies
setnames(DT,"b","B") # by name, no match() needed (warning if "b" is missing)
setnames(DT,3,"C") # by position with warning if 3 > ncol(DT)
setnames(DT,2:3,c("D","E")) # multiple
setnames(DT,c("a","E"),c("A","F")) # multiple by name (warning if either "a" or "E" is missing)
setnames(DT,c("X","Y","Z")) # replace all (length of names must be == ncol(DT))
DT <- data.table(x = 1:3, y = 4:6, z = 7:9)
setnames(DT, -2, c("a", "b")) # NEW FR #1443, allows -ve indices in 'old' argument
DT = data.table(a=1:3, b=4:6)
f = function(...) {
# ...
setattr(DT,"myFlag",TRUE) # by reference
# ...
localDT = copy(DT)
setattr(localDT,"myFlag2",TRUE)
# ...
invisible()
}
f()
attr(DT,"myFlag") # TRUE
attr(DT,"myFlag2") # NULL
# }
Run the code above in your browser using DataLab