我正在嘗試向我的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