抑制data.table j函数中的输出

时间:2012-11-09 23:00:43

标签: r plot data.table correlation

我想使用ccfid中为data.table变量的不同值绘制一些R图。问题是ccf返回的值与data.table不一致。我不关心返回值,只想看实际情节。一些代码:

require(data.table)
x <- data.table(id=rep(1:10, each=10), a=rep(1:10,10), b=rep(10:1,10))
x[,ccf(a,b),by=id]
Error in `[.data.table`(x, , ccf(a, b), by = id) : 
All items in j=list(...) should be atomic vectors or lists. If you are trying something like j=list(.SD,newcol=mean(colA)) then use := by group instead (much quicker), or cbind or merge afterwards.

2 个答案:

答案 0 :(得分:6)

在问题中给出了这个:

  

我不关心返回值,只想看实际情节。

然后:

x[, {ccf(a,b);NULL}, by=id]

答案 1 :(得分:3)

这似乎相当丑陋(它最终)返回一个矩阵:

xl <- x[,list(ccf(a,b)),by=id] # ugly mess
sapply(seq(1, 60, by=6), function(vecid) xl[[2]][[vecid]] )  # nice neat matrix

这可能更漂亮:

xl <- x[ , ccf(a,b)$acf, by=id]
xl
     id          V1
  1:  1  0.37575758
  2:  1  0.25757576
  3:  1  0.07878788
  4:  1 -0.14848485
  5:  1 -0.41212121
 ---               
126: 10 -0.41212121
127: 10 -0.14848485
128: 10  0.07878788
129: 10  0.25757576
130: 10  0.37575758
相关问题