我是 data.table 的新手,我遇到了这个类的问题。我有一个data1
包含 2 列的表 ( ):Couple
和Ratio
. Couple
是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
所以我可以在通过索引访问元素时更改值,但在通过值访问值时不能。我怎样才能做到这一点?