data.table,R中的copy()

时间:2012-09-01 16:30:10

标签: r data.table

在我对data.table进行子集并将返回值分配给另一个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 {{1在通过引用设置列之前?

由于

1 个答案:

答案 0 :(得分: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