[datatable-help] := construct doesn't seem to work in lists of data.tables

Bacou, Melanie mel at mbacou.com
Wed Aug 15 06:48:19 CEST 2012


Hello,

I just noticed an odd behavior with lists of data.tables:

dt1 <- data.table(a=1:3, b=4:6, c=7:9)
dt2 <- data.table(a=10:12, b=13:15, c=16:18)

# Combine in a list
myList <- list(dt1, dt2)

# Adding a new column to first data.table -- this doesn't work
myList[[1]][, d := 4:6]
#    a b c d
# 1: 1 4 7 4
# 2: 2 5 8 5
# 3: 3 6 9 6
# Warning message:
# In `[.data.table`(myList[[1]], , `:=`(d, 4:6)) :
#   Invalid .internal.selfref detected and fixed by taking a copy of the whole table, 
so that := can add this new column by reference. At an earlier point, this data.table 
has been copied by R. Avoid key<-, names<- and attr<- which in R currently (and oddly) 
all copy the whole data.table. Use set* syntax instead to avoid copying: setkey(), 
setnames() and setattr(). If this message doesn't help, please report to 
datatable-help so the root cause can be fixed.

myList[[1]]
#    a b c
# 1: 1 4 7
# 2: 2 5 8
# 3: 3 6 9

# I need to reassign -- this works
myList[[1]] <- myList[[1]][, d := 4:6]

myList[[1]]
#    a b c d
# 1: 1 4 7 4
# 2: 2 5 8 5
# 3: 3 6 9 6

# But on the other hand this works no problem
setcolorder(myList[[1]], 4:1)
myList[[1]]
#    d c b a
# 1: 4 7 4 1
# 2: 5 8 5 2
# 3: 6 9 6 3

Is this normal behavior, seems a bit odd to me?

Here is my session:

 > sessionInfo()
R version 2.15.1 (2012-06-22)
Platform: x86_64-redhat-linux-gnu (64-bit)

locale:
[1] C

attached base packages:
[1] stats     graphics  utils     datasets  grDevices methods base

other attached packages:
[1] foreign_0.8-50      RJDBC_0.2-0         DBI_0.2-5
[4] XLConnect_0.2-0     XLConnectJars_0.2-0 rJava_0.9-3
[7] data.table_1.8.2    rj_1.1.0-4

loaded via a namespace (and not attached):
[1] rj.gd_1.1.0-1 tools_2.15.1


Thanks very much for this fantastic package!

--Mel.

Melanie BACOU
International Food Policy Research Institute
Agricultural Economist, HarvestChoice
E-mail mel at mbacou.com <mailto:mel at mbacou.com>
Visit harvestchoice.org <http://www.harvestchoice.org/>



More information about the datatable-help mailing list