我是 data.table 的新手,我对这个类有疑问。我有一张桌子(data1
)有 2 列:Couple
and Ratio
. Couple
is the Key
data.table 的。我正在尝试修改表中的值。
在下面的代码中,cple
是现有值Couple
。当我运行它时,我得到评论中显示的结果:
data1[cple]$Ratio[1]<-0 # I get more than 50 warnings and it doesn't work
data1$Ratio[1]<-0 # It works perfectly (but it's not the same as the above code)
该错误似乎与 Keys 有关,但我无法弄清楚它的含义。
下面是一个例子:
>data1<-data.table(Couple=c("a","a","b","b"),Ratio=1:4)
>data1
Couple Ratio
1: a 1
2: a 2
3: b 3
4: b 4
>setkey(data1,Couple)
>data1["a"]$Ratio[1]<-2 #doesn't work warning message
WARNING:
#In `[<-.data.table`(`*tmp*`, "a", value = list(Couple = c("a", "a" :
# Coerced 'double' RHS to 'integer' to match the column's type; may have truncated precision. Either change the target column to 'double' first (by creating a new 'double' vector length 4 (nrows of entire table) and assign that; i.e. 'replace' column), or coerce RHS to 'integer' (e.g. 1L, NA_[real|integer]_, as.*, etc) to make your intent clear and for speed. Or, set the column type correctly up front when you create the table and stick to it, please.
>data1$Ratio[1]<-2 #works
>data1
Couple Ratio
1: a 2
2: a 2
3: b 3
4: b 4
因此,我可以在通过元素的索引访问元素时更改该值,但在通过其值访问该值时则不能更改该值。我怎样才能做到这一点?