【发布时间】:2016-04-07 22:18:13
【问题描述】:
我试图将 data.table b 中“True_value”列中的值分配到 data.table a 中的同名列中。我终于得到了一些工作,但我不确定 1)它为什么工作以及 2)是否有更好的方法。任何见解将不胜感激。是的,恕我直言,我已经阅读了一些 data.table。
require(data.table)
a=as.matrix(c("a","b","c"))
a=cbind(a,c("yes", "no", "maybe"))
a=cbind(a,c("NA","NA","NA"))
rownames(a)=c("one", "two","three")
colnames(a)=c("letter", "status", "True_value")
a=as.data.table(a)
b=as.data.table(c(3,13,42))
colnames(b)=c("True_value")
a[,True_value]
b[,True_value]
##this doesn't work
a[,True_value] = b[,True_value]
##this doesn't assign the values, but rather assigns the string, "True_value"
a[,"True_value"] = b[,"True_value"]
##this doesn't work
a[,.(True_value)] = b[,.(True_value)]
##none of these work
a[,.(True_value)] = unlist(b[,True_value])
a[,True_value] = unlist(b[,True_value])
##and yet this one works. Why does this work, and is there a better way to do this?
a[,"True_value"] = unlist(b[,True_value])
【问题讨论】:
标签: r data.table