You need to enable JavaScript to run this app. R语言中对迭代列表内第二年district变量加权后求跨迭代均值
优惠活动
大模型
产品
解决方案
定价
更多

R语言中对迭代列表内第二年district变量加权后求跨迭代均值

R 批量计算分组加权求和的迭代均值

问题背景

我有一个名为df的列表,包含3组迭代的两年预测数据,具体结构如下:

iteration1 <- list(year1 = data.frame(age = c(10, 11, 12, 13),
                                      district = c(1, 2, 3, 4),
                                      gender = c(1, 2, 2, 1),
                                      weight = c(12.2, 11.3, 11.2, 10.1)),
                   year2 = data.frame(age = c(10, 11, 12, 13, 10, 10),
                                      district = c(1, 2, 3, 4, 2, 1),
                                      gender = c(1, 2, 2, 1, 1, 1),
                                      weight = c(12.2, 11.3, 11.2, 10.1, 12.2, 13.1)))

iteration2 <- list(year1 = data.frame(age = c(10, 11, 12, 13), 
                                      district = c(1, 2, 3, 4),
                                      gender = c(2, 2, 1, 1),
                                      weight = c(12.2, 11.3, 11.2, 10.1)),
                   year2 = data.frame(age = c(10, 11, 12, 13, 13, 13, 12),
                                      district = c(1, 2, 3, 4, 1, 3, 3),
                                      gender = c(2, 2, 1, 1, 2, 2, 2),
                                      weight = c(12.2, 11.3, 11.2, 10.1, 10.9, 11.9, 15.1)))

iteration3 <- list(year1 = data.frame(age = c(10, 11, 12, 13),
                                      district = c(1, 2, 3, 4),
                                      gender = c(2, 2, 1, 1),
                                      weight = c(12.2, 11.3, 11.2, 10.1)),
                   year2 = data.frame(age = c(10, 11, 12, 13, 10, 10, 11, 12),
                                      district = c(1, 2, 3, 4, 4, 3, 2, 2),
                                      gender = c(2, 2, 1, 1, 2, 2, 1, 2),
                                      weight = c(12.2, 11.3, 11.2, 10.1, 13.5, 12.8, 13.9, 14.9)))

df <- list(iteration1 = iteration1, iteration2 = iteration2, iteration3 = iteration3)

需求说明

仅对每组迭代中year2的district变量,基于对应行的weight变量执行加权求和,最终得到3组迭代下各district加权值的算术均值,预期输出格式如下:

district     mean of each district for all three iterations
1                           20.2          
2                           24.96 
3                           24.46
4                           14.6

目前我通过重复调用wtd.table(df$iterationN$year2$district,weights=df$iterationN$year2$weight)计算每组迭代的加权结果,得到三组结果如下:

1    2    3    4 
25.3 23.5 11.2 10.1 
  1    2    3    4 
23.1 11.3 38.2 10.1  
1    2    3    4 
12.2 40.1 24.0 23.6 

再手动对同district的三个结果求均值,例如mean(25.3,23.1,12.2),请问如何通过代码批量完成该计算?

解决方法

方法1:基础R实现(无额外包依赖)

# 遍历所有迭代,计算每个迭代下year2各district的加权和
wtd_sums <- lapply(df, function(x) {
  tapply(x$year2$weight, x$year2$district, sum)
})
# 合并结果后按district维度求均值
res <- data.frame(
  district = names(wtd_sums[[1]]),
  mean_value = rowMeans(do.call(cbind, wtd_sums))
)
# 输出结果
res

如果要沿用你习惯的wtd.table函数,直接替换计算逻辑即可:

library(questionr)
wtd_sums <- lapply(df, function(x) {
  wtd.table(x$year2$district, weights = x$year2$weight)
})
res <- data.frame(
  district = names(wtd_sums[[1]]),
  mean_value = rowMeans(do.call(cbind, wtd_sums))
)

方法2:tidyverse风格实现(可读性更高)

library(purrr)
library(dplyr)

res <- map_dfr(df, ~.x$year2, .id = "iteration") %>%
  group_by(iteration, district) %>%
  summarise(wtd_sum = sum(weight), .groups = "drop") %>%
  group_by(district) %>%
  summarise(mean_value = mean(wtd_sum), .groups = "drop")

res

两种方法输出的结果和你给出的预期数值完全匹配。

内容的提问来源于stack exchange,提问作者mehmo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 03:24:04