gpt4 book ai didi

r - 按位置排列的列表元素的平均值

转载 作者:行者123 更新时间:2023-12-01 16:51:35 25 4
gpt4 key购买 nike

假设我有一个这样的数字向量列表:

lst45 <- structure(list(CanESM2 = structure(c(75172.3232265472, 68514.4574398041, 70116.8608039856, 74918.7828659058, 74355.116765213, 70350.6421344757, 67336.5649261475, 71819.5331493378, 71239.2236221314), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), `GFDL-ESM2M` = structure(c(74736.6871696472, 74668.9771896362, 70693.9238479614, 72538.5751586914, 68865.4270866394, 76652.1024021149, 72507.3696491241, 72044.253433609, 70758.3188240051), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), inmcm4 = structure(c(64797.6587924957, 70213.8876274109, 72865.955796051, 72756.7691856384, 76886.3437725067, 78064.3871330261, 70558.1268108368, 71354.149344635, 72268.3041442871), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), `MRI-CGCM3` = structure(c(83597.9063896179, 79029.3697322845, 83841.0448295593, 82892.550799942, 75588.5374900818, 74541.5410072327, 84481.3175567627, 74491.1396362305, 79476.2405437469), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9")), `NorESM1-M` = structure(c(77061.6410392761, 73100.7215652466, 74400.8042865753, 69916.0041610718, 71969.7732620239, 70910.7214992523, 68759.73670578, 67605.5678882599, 66542.3943050385), .Names = c("layer.1", "layer.2", "layer.3", "layer.4", "layer.5", "layer.6", "layer.7", "layer.8", "layer.9"))), .Names = c("CanESM2", "GFDL-ESM2M", "inmcm4", "MRI-CGCM3", "NorESM1-M"))

# $CanESM2
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9
# 75172.32 68514.46 70116.86 74918.78 74355.12 70350.64 67336.56 71819.53 71239.22
#
# $`GFDL-ESM2M`
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9
# 74736.69 74668.98 70693.92 72538.58 68865.43 76652.10 72507.37 72044.25 70758.32
#
# $inmcm4
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9
# 64797.66 70213.89 72865.96 72756.77 76886.34 78064.39 70558.13 71354.15 72268.30
#
# $`MRI-CGCM3`
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9
# 83597.91 79029.37 83841.04 82892.55 75588.54 74541.54 84481.32 74491.14 79476.24
#
# $`NorESM1-M`
# layer.1 layer.2 layer.3 layer.4 layer.5 layer.6 layer.7 layer.8 layer.9
# 77061.64 73100.72 74400.80 69916.00 71969.77 70910.72 68759.74 67605.57 66542.39

我需要计算所有五个“layer.1”、所有五个“layer.2”等的平均值。生成的对象将是一个向量。

仅使用 native R 函数执行此操作的最佳(更清晰)方法是什么?

最佳答案

这可以通过多种方式实现。我会推荐其中两个。第一个:

colMeans(do.call(rbind,lst45))

这里列表的所有元素被 rbind 结合在一起形成一个 matrix 我们可以调用 colMeans 来获得平均值每一列。

另一种方式:

Reduce(`+`,lst45)/length(lst45)

列表的第一个向量与第二个向量相加,结果与第三个向量相加,依此类推。我们最后除以列表的元素数量来获得平均值。

关于r - 按位置排列的列表元素的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35002152/

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