r - r 平均列表的元素
问题描述
假设我有一个包含三个对象的列表,如下所示
[[1]]
yeargp gender Estimate ci.lower ci.upper
1 1991-1995 M 0.8757711 -0.8407402 2.592282
2 1991-1995 F 0.0000000 0.0000000 0.000000
3 1996-2000 M 2.2119671 -0.8536629 5.277597
4 1996-2000 F 2.8254349 -0.3718457 6.022715
5 2001-2005 M 7.7695653 2.6460791 12.893051
6 2001-2005 F 2.2710074 -0.3108077 4.852822
7 2006-2010 M 12.1639403 6.1435827 18.184298
8 2006-2010 F 6.3637686 2.5667028 10.160834
[[2]]
yeargp gender Estimate ci.lower ci.upper
1 1991-1995 M 0.000000 0.0000000 0.000000
2 1991-1995 F 0.000000 0.0000000 0.000000
3 1996-2000 M 2.211967 -0.8536629 5.277597
4 1996-2000 F 2.825435 -0.3718457 6.022715
5 2001-2005 M 8.599076 3.2238115 13.974341
6 2001-2005 F 1.517900 -0.6003366 3.636137
7 2006-2010 M 13.485237 7.1911854 19.779289
8 2006-2010 F 5.991342 2.2651006 9.717582
[[3]]
yeargp gender Estimate ci.lower ci.upper
1 1991-1995 M 0.000000 0.0000000 0.000000
2 1991-1995 F 0.000000 0.0000000 0.000000
3 1996-2000 M 3.317951 -0.4366640 7.072565
4 1996-2000 F 1.883623 -0.7269454 4.494192
5 2001-2005 M 7.643263 2.6144621 12.672065
6 2001-2005 F 2.366219 -0.3266446 5.059082
7 2006-2010 M 13.637280 7.2795528 19.995008
8 2006-2010 F 5.991342 2.2651006 9.717582
计算 column3-column5 (Estimate, ci.lower, ci.upper) 中所有元素的平均值的有效方法是什么?
这是我期望实现的。
year Gender Estimate L.C.L U.C.L
1991-1995 M 0.2919237 -0.280246733 0.864094
1991-1995 F 0 0 0
1996-2000 M 2.580628367 -0.714663267 5.875919667
1996-2000 F 2.511497633 -0.490212267 5.513207333
2001-2005 M 8.0039681 2.828117567 13.179819
2001-2005 F 2.0517088 -0.4125963 4.516013667
2006-2010 M 13.09548577 6.8714403 19.31953167
2006-2010 F 6.1154842 2.365634667 9.865332667
非常感谢任何建议。下面是我列表中 dput 函数的输出。
templist <- list(structure(list(yeargp = structure(c(1L, 1L, 2L, 2L, 3L,
3L, 4L, 4L), .Label = c("1991-1995", "1996-2000", "2001-2005",
"2006-2010"), class = "factor"), gender = structure(c(1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L), .Label = c("M", "F"), class = "factor"),
Estimate = c(0.875771052955988, 0, 2.2119670520759, 2.82543488793347,
7.76956525829443, 2.27100738124732, 12.1639402903974, 6.36376856610303
), ci.lower = c(-0.840740210837749, 0, -0.853662876400907,
-0.371845674593782, 2.64607905876294, -0.310807679155956,
6.14358267928312, 2.56670275678554), ci.upper = c(2.59228231674973,
0, 5.2775969805527, 6.02271545046073, 12.8930514578259, 4.85282244165059,
18.1842979015118, 10.1608343754205)), .Names = c("yeargp",
"gender", "Estimate", "ci.lower", "ci.upper"), row.names = c(NA,
-8L), class = "data.frame"), structure(list(yeargp = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("1991-1995", "1996-2000",
"2001-2005", "2006-2010"), class = "factor"), gender = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("M", "F"), class = "factor"),
Estimate = c(0, 0, 2.2119670520759, 2.82543488793347, 8.59907630432197,
1.51790034439859, 13.4852371898016, 5.9913415231189), ci.lower = c(0,
0, -0.853662876400907, -0.371845674593782, 3.2238114821611,
-0.600336642205772, 7.19118540022504, 2.26510058455415),
ci.upper = c(0, 0, 5.2775969805527, 6.02271545046073, 13.9743411264828,
3.63613733100296, 19.7792889793781, 9.71758246168364)), .Names = c("yeargp",
"gender", "Estimate", "ci.lower", "ci.upper"), row.names = c(NA,
-8L), class = "data.frame"), structure(list(yeargp = structure(c(1L,
1L, 2L, 2L, 3L, 3L, 4L, 4L), .Label = c("1991-1995", "1996-2000",
"2001-2005", "2006-2010"), class = "factor"), gender = structure(c(1L,
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("M", "F"), class = "factor"),
Estimate = c(0, 0, 3.31795057811384, 1.88362325862232,
7.6432632822894, 2.36621893284824, 13.6372803202135, 5.9913415231189
), ci.lower = c(0, 0, -0.436663954372684, -0.726945388947865,
2.6144620600312, -0.32664459212626, 7.27955279689059, 2.26510058455415
), ci.upper = c(0, 0, 7.07256511060037, 4.4941919061925,
12.6720645045476, 5.05908245782275, 19.9950078435365, 9.71758246168364
)), .Names = c("yeargp", "gender", "Estimate", "ci.lower",
"ci.upper"), row.names = c(NA, -8L), class = "data.frame"))
解决方案
简短而甜蜜:
df <- do.call(rbind, templist)
aggregate(df[3:5], df[1:2], mean)
推荐阅读
- node.js - Node.js 不会运行 localhost,webpack-dev-server 和 cardkit 的依赖问题
- inventory-management - OCS 清单是否支持远程 WMI?
- java - Autowired 在 Spring Boot 中返回 null
- c# - .Net 中的反应式 Rx 压缩队列
- tensorflow - 检查 TensorFlow 安装是否支持 XLA
- java - 我无法使用 Dagger2 将我的课程注入我的 ViewHolder
- javascript - 从 html 和 javascript 中的下拉选择中调用函数
- deployment - .NET CORE 通过命令行发布文件夹配置文件不起作用
- c++ - 如何在 Windows 上使用 Visual Studio 2017 连接到 C++ 中的 sqlite 数据库?
- url - URL.getText() 是否总是关闭连接?