Question

I am having a little trouble understanding the behavior of the following in data.table...

##Combine into one data.table
RecordNo <- 1:36
Record_Date <- c(31,33,38,41,44,59,68,69,75,78,85,88,
                         32,34,45,46,51,54,60,65,67,70,74,80,
                         33,35,42,45,50,60,65,70,75,80,82,85)
Cust_ID <- c(rep(1,12),rep(2,12),rep(3,12))
data <- data.table(RecordNo,Record_Date,Cust_ID)

##Create "list" of comparison dates
data[,list(Compare_Date=list(Record_Date)),by=c("Cust_ID")]

   Cust_ID       Compare_Date
1:       1 33,35,42,45,50,60,
2:       2 33,35,42,45,50,60,
3:       3 33,35,42,45,50,60,

The above code outputs the date list for Cust_ID=3 for each Cust_ID. I desire an output like this..

   Cust_ID       Compare_Date
1:       1 31,33,38,41,44,59,
2:       2 32,34,45,46,51,54,
3:       3 33,35,42,45,50,60,

Any ideas why data.table is returning the list of dates for Cust_ID 3 instead of the correct list of values for each Cust_ID?

sessionInfo()

R version 3.1.0 beta (2014-03-28 r65330)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

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

other attached packages:
[1] data.table_1.9.2

loaded via a namespace (and not attached):
[1] plyr_1.8.1     Rcpp_0.11.0    reshape2_1.2.2 stringr_0.6.2  tools_3.1.0 
Was it helpful?

Solution

Update: The behaviour DT[, list(list(.)), by=.] sometimes resulted in wrong results in R version >= 3.1.0. This is now fixed in commit #1280 in the current development version of data.table v1.9.3. From NEWS:

  • DT[, list(list(.)), by=.] returns correct results in R >=3.1.0 as well. The bug was due to recent (welcoming) changes in R v3.1.0 where list(.) does not result in a copy. Closes #481.

With this fix, there's no need for the I() anymore.


Seems to work if I put in an "I" before the list:

##Create "list" of comparison dates
data[,list(Compare_Date=list(I(Record_Date))),by=c("Cust_ID")]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top