问题描述
我对data.table中的以下行为有些麻烦。
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,
上述代码输出日期列表对于每个Cust_ID,Cust_ID = 3。我希望有这样的输出。
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,
data.table返回Cust_ID 3的日期列表而不是每个Cust_ID的正确值列表的任何想法?
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
推荐答案
更新: c $ c> DT [,list(list(。)),通过=。] 有时导致错误的结果在R版本> = 3.1.0。现在,在中修正了 v1.9.3。从:
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:
有了这个修复,不再需要 I()
。
With this fix, there's no need for the I()
anymore.
如果我在列表之前加入I,似乎可以工作:
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")]
这篇关于需要帮助了解r data.table行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!