I get a warning when I use :=
right after converting all data.frames to data.tables:
library(data.table) #Win R-3.5.1 x64 data.table_1.12.2
df1 <- data.frame(A=1, B=2)
df2 <- data.frame(D=3)
lapply(mget(ls()), function(x) {
if (is.data.frame(x)) {
setDT(x)
}
})
df1[, rn:=.I]
Warning message: In
[.data.table
(df1, ,:=
(rn, .I)) : Invalid .internal.selfref detected and fixed by taking a (shallow) copy of the data.table so that := can add this new column by reference. At an earlier point, this data.table has been copied by R (or was created manually using structure() or similar). Avoid names<- and attr<- which in R currently (and oddly) may copy the whole data.table. Use set* syntax instead to avoid copying: ?set, ?setnames and ?setattr. If this message doesn't help, please report your use case to the data.table issue tracker so the root cause can be fixed or this message improved.
The below also generates the same warning:
df3 <- data.frame(E=3)
df4 <- data.frame(FF=4)
for (l in list(df3, df4)) setDT(l)
df3[, rn:=.I]
Typing one by one works but tedious
df5 <- data.frame(G=5)
setDT(df5)
df[, rn := .I] #no warning
What is the idiomatic way to convert all data.frames to data.tables?
Related: