1

data.table在我对 a 进行子集化并将返回值分配给另一个之后,我试图通过引用分配一列data.table(玩具示例):

> x <- data.table(a=1:10, b=11:20, c=21:30)
> x
     a  b  c
 1:  1 11 21
 2:  2 12 22
 3:  3 13 23
 4:  4 14 24
 5:  5 15 25
 6:  6 16 26
 7:  7 17 27
 8:  8 18 28
 9:  9 19 29
10: 10 20 30
> y <- x[a==1 | a == 2, list(a,b,c)]
> y[,d:=a+b]
Error in `[.data.table`(y, , `:=`(d, a + b)) : 
  It appears that at some earlier point, names of this data.table have been reassigned.     Please ensure to use setnames() rather than names<- or colnames<-. Otherwise, please report     to datatable-help.

我不完全理解这个问题:返回y的只是一个“视图”到相同的内存中x,因此copy x在通过引用设置列之前应该这样做吗?

谢谢

4

1 回答 1

3

无法使用 R 2.15.1 中的 data.table 1.8.2 重现错误:

> x <- data.table(a=1:10, b=11:20, c=21:30); x
     a  b  c
 1:  1 11 21
 2:  2 12 22
 3:  3 13 23
 4:  4 14 24
 5:  5 15 25
 6:  6 16 26
 7:  7 17 27
 8:  8 18 28
 9:  9 19 29
10: 10 20 30
> 
>  y <- x[x$a==1 | x$a == 2, list(a,b,c)]
> 
> y
   a  b  c
1: 1 11 21
2: 2 12 22

> y[,d:=a+b]
   a  b  c  d
1: 1 11 21 12
2: 2 12 22 14
于 2012-09-02T01:20:11.527 回答