Maybe some can tell me why the names I assigned to "idVars" are changing after adding a column to my data.table (without reassigning them)? How can I persist the assignment to store only the first two column names?
Thanks!
library(data.table)
DT <- data.table(a=1:10, b=1:10)
idVars <- names(DT)
print(idVars)
# [1] "a" "b"
DT[, "c" := 1:10]
print(idVars)
# [1] "a" "b" "c"
# devtools::session_info()
# data.table * 1.11.6 2018-09-19 CRAN (R 3.5.1)
We can create a copy of the names as the names(DT) and the 'idVars' have the same memory location
tracemem(names(DT))
#[1] "<0x7f9d74c99600>"
tracemem(idVars)
#[1] "<0x7f9d74c99600>"
So, instead create a copy of the names
idVars <- copy(names(DT))
tracemem(idVars)
#[1] "<0x7f9d7d2b97c8>"
and it wouldn't change after the assignment
DT[, "c" := 1:10]
idVars
#[1] "a" "b"
According to ?copy:
A
copy()may be required when doingdt_names = names(DT). Due to R's copy-on-modify,dt_namesstill points to the same location in memory asnames(DT). Therefore modifyingDTby reference now, say by adding a new column,dt_nameswill also get updated. To avoid this, one has to explicitly copy:dt_names <- copy(names(DT)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With