我正在寻找一种使用data.table计算变量加权总和的解决方案。我希望这个例子足够清晰易懂。
require(data.table)
dt <- data.table(matrix(1:200, nrow = 10))
dt[, gr := c(rep(1,5), rep(2,5))]
dt[, w := 2]
# Error: object 'w' not found
dt[, lapply(.SD, function(x) sum(x * w)),
.SDcols = paste0("V", 1:4)]
# Error: object 'w' not found
dt[, lapply(.SD * w, sum),
.SDcols = paste0("V", 1:4)]
# This works with out groups
dt[, lapply(.SD, function(x) sum(x * dt$w)),
.SDcols = paste0("V", 1:4)]
# It does not work by groups
dt[, lapply(.SD, function(x) sum(x * dt$w)),
.SDcols = paste0("V", 1:4), keyby = gr]
# The result to be expected
dt[, list(V1 = sum(V1 * w),
V2 = sum(V2 * w),
V3 = sum(V3 * w),
V4 = sum(V4 * w)), keyby = gr]
### from Aruns answer
dt[, lapply(.SD[, paste0("V", 1:4), with = F],
function(x) sum(x*w)), by=gr]
w*w
列即可。 - Roland{}
,否则找不到w
。我会进行编辑。也许你知道为什么... - Arunprint(dt[, lapply(.SD, function(x, w) sum(x*w), w=w), by=gr][, w := NULL])
- Rolanddt[, lapply(.SD, function(x, w) sum(x*w), w=w), by=gr, .SDcols=c("w", "V1", "V2", "V3", "V4")][, w := NULL]
- Arun