我正在尝试向我的data.table
添加列,其中名称是动态的。另外,我需要在添加这些列时使用by
参数。例如:data.table中的动态列名
test_dtb <- data.table(a = sample(1:100, 100), b = sample(1:100, 100), id = rep(1:10,10))
cn <- parse(text = "blah")
test_dtb[ , eval(cn) := mean(a), by = id]
# Error in `[.data.table`(test_dtb, , `:=`(eval(cn), mean(a)), by = id) :
# LHS of := must be a single column name when with=TRUE. When with=FALSE the LHS may be a vector of column names or positions.
的另一种尝试:从马修
cn <- "blah"
test_dtb[ , cn := mean(a), by = id, with = FALSE]
# Error in `[.data.table`(test_dtb, , `:=`(cn, mean(a)), by = id, with = FALSE) : 'with' must be TRUE when 'by' or 'keyby' is provided
更新:
这现在在v1.8.3的R-伪造。感谢您的突出!
见新的实例中,这类似的问题:
Assign multiple columns using data.table, by group
太棒了,谢谢。我可以发誓我尝试了这种变化,但显然我没有。非常感谢。 – Alex 2012-07-31 17:52:09
+1将此问题的链接添加到了[FR#2120](https://r-forge.r-project.org/tracker/index.php?func=detail&aid=2120&group_id=240&atid=978)。似乎会出现很多。 – 2012-08-09 13:26:34