我之前曾问过这个问题,并认为我可能会发布一个示例来说明为什么我会看到这种效果,以防它可能会有所帮助:
require(data.table)
x <- data.table(a=1:10)
x[,a:=scale(a)]
# [,1]
# 1: -1.4863011
# 2: -1.1560120
# 3: -0.8257228
# 4: -0.4954337
# 5: -0.1651446
# 6: 0.1651446
# 7: 0.4954337
# 8: 0.8257228
# 9: 1.1560120
#10: 1.4863011
na.omit(x)
Error in `[.data.table`(object, !omit) :
i is invalid type (matrix). Perhaps in future a 2 column matrix could return a list of elements of DT (in the spirit of A[B] in FAQ 2.14). Please let datatable-help know if you'd like this, or add your comments to FR #1611.
原因似乎是scale
不返回a vector
,data.table
也没有抱怨。相反,这样做x[,a:=as.vector(scale(a))]
似乎可以解决问题。我错过了文档中的某些内容吗?