gpt4 book ai didi

r - data.table 中的动态列名

转载 作者:行者123 更新时间:2023-12-03 08:39:38 25 4
gpt4 key购买 nike

我正在尝试向我的 data.table 添加列,其中名称是动态的。我另外我需要使用by添加这些列时的参数。例如:

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

马修更新:

这现在适用于 R-Forge 上的 v1.8.3。谢谢你的强调!
有关新示例,请参阅此类似问题:

Assign multiple columns using data.table, by group

最佳答案

来自 data.table 1.9.4 ,你可以这样做:

## A parenthesized symbol, `(cn)`, gets evaluated to "blah" before `:=` is carried out
test_dtb[, (cn) := mean(a), by = id]
head(test_dtb, 4)
# a b id blah
# 1: 41 19 1 54.2
# 2: 4 99 2 50.0
# 3: 49 85 3 46.7
# 4: 61 4 4 57.1
详情?:= :

DT[i, (colvector) := val]

[...] NOW PREFERRED [...] syntax. The parens are enough to stop the LHS being a symbol; same as c(colvector)



原答案:
您走在了正确的轨道上:构建一个表达式以在对 [.data.table 的调用中进行评估是 数据表做这种事情的方式。更进一步,为什么不构造一个计算结果为整个 j 的表达式?参数(而不仅仅是它的左侧)?
像这样的事情应该可以解决问题:
## Your code so far
library(data.table)
test_dtb <- data.table(a=sample(1:100, 100),b=sample(1:100, 100),id=rep(1:10,10))
cn <- "blah"

## One solution
expr <- parse(text = paste0(cn, ":=mean(a)"))
test_dtb[,eval(expr), by=id]

## Checking the result
head(test_dtb, 4)
# a b id blah
# 1: 30 26 1 38.4
# 2: 83 82 2 47.4
# 3: 47 66 3 39.5
# 4: 87 23 4 65.2

关于r - data.table 中的动态列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11745169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com