data.table
这是我使用“ ”包设法实现的目标:
DT <- data.table(dat, key = "key")
DT[, list(v1 = sum(rate * v1)/sum(rate), v2 = sum(rate * v2)/sum(rate)), by = "key"]
# key v1 v2
# 1: a 3.333333 6.666667
# 2: b 0.600000 2.400000
好的。所以这很容易写出两个变量,但是当我们有更多的列时呢。lapply(.SD,...)
与您的功能结合使用:
首先,一些数据:
set.seed(1)
dat <- data.frame(key = rep(c("a", "b"), times = 10),
rate = runif(20, min = 0, max = 1),
v1 = sample(10, 20, replace = TRUE),
v2 = sample(20, 20, replace = TRUE),
v3 = sample(30, 20, replace = TRUE),
x1 = sample(5, 20, replace = TRUE),
x2 = sample(6:10, 20, replace = TRUE),
x3 = sample(11:15, 20, replace = TRUE))
library(data.table)
datDT <- data.table(dat, key = "key")
datDT
# key rate v1 v2 v3 x1 x2 x3
# 1: a 0.26550866 10 17 28 3 9 15
# 2: a 0.57285336 7 16 14 2 7 13
# 3: a 0.20168193 3 11 20 4 9 14
# 4: a 0.94467527 1 1 15 4 6 13
# 5: a 0.62911404 9 15 3 2 10 12
# 6: a 0.20597457 5 10 11 2 10 13
# 7: a 0.68702285 5 9 11 4 7 11
# 8: a 0.76984142 9 2 15 4 6 15
# 9: a 0.71761851 8 7 26 3 9 13
# 10: a 0.38003518 8 14 24 5 8 15
# 11: b 0.37212390 3 13 9 4 7 13
# 12: b 0.90820779 2 12 10 2 10 11
# 13: b 0.89838968 4 16 8 2 7 13
# 14: b 0.66079779 4 10 23 1 8 12
# 15: b 0.06178627 4 14 27 1 8 13
# 16: b 0.17655675 6 18 26 1 9 11
# 17: b 0.38410372 2 5 11 5 8 14
# 18: b 0.49769924 7 2 27 4 6 13
# 19: b 0.99190609 2 11 12 3 6 13
# 20: b 0.77744522 5 9 29 4 9 13
二、聚合:
datDT[, lapply(.SD, function(x, y = rate) sum(y * x)/sum(y)), by = "key"]
# key rate v1 v2 v3 x1 x2 x3
# 1: a 0.6501303 6.335976 8.634691 15.75915 3.363832 7.658762 13.19152
# 2: b 0.7375793 3.595585 10.749705 16.26582 2.792390 7.741787 12.57301
如果您有一个非常大的数据集,您可能想要进行data.table
一般探索。
对于它的价值,我在基础 R 中也取得了成功,但我不确定这会有多有效,特别是因为转置等等。
t(sapply(split(dat, dat[1]),
function(x, y = 3:ncol(dat)) {
V1 <- vector()
for (i in 1:length(y)) {
V1[i] <- sum(x[2] * x[y[i]])/sum(x[2])
}
V1
}))
# [,1] [,2] [,3] [,4] [,5] [,6]
# a 6.335976 8.634691 15.75915 3.363832 7.658762 13.19152
# b 3.595585 10.749705 16.26582 2.792390 7.741787 12.57301