需要帮助了解r data.table行为

时间:2014-04-16 21:49:30

标签: r data.table

我在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的日期列表。我希望像这样的输出..

   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的正确值列表?

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 

1 个答案:

答案 0 :(得分:1)

更新:行为DT[, list(list(.)), by=.]有时会导致R版本&gt; = 3.1.0的错误结果。现在,commit #1280 v1.9.3的当前开发版本中的data.table已修复此问题。来自NEWS

  
      
  • DT[, list(list(.)), by=.]也会在R&gt; = 3.1.0中返回正确的结果。该错误是由于最近(欢迎)R v3.1.0中的更改导致list(.)不会导致副本。关闭#481
  •   

使用此修复程序,不再需要I()


如果我放入&#34;我&#34;在列表之前:

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