R中列表中索引元素的平均值
我有一个与本文中提出的问题类似的问题,但是我对获取列表中的elementwise平均值不感兴趣,而是对列表中元素中的每个索引值感兴趣 给我这三张单子R中列表中索引元素的平均值,r,list,mean,R,List,Mean,我有一个与本文中提出的问题类似的问题,但是我对获取列表中的elementwise平均值不感兴趣,而是对列表中元素中的每个索引值感兴趣 给我这三张单子 ice_2000 = list(seq(1,5,1),seq(6,10,1),seq(11,15,1)) ice_1990 = list(seq(1,5,1),seq(6,10,1),seq(11,15,1)) ice_1980 = list(seq(1,5,1),seq(6,10,1),seq(11,15,1)) 我想找到每个站点每天几年的平均
ice_2000 = list(seq(1,5,1),seq(6,10,1),seq(11,15,1))
ice_1990 = list(seq(1,5,1),seq(6,10,1),seq(11,15,1))
ice_1980 = list(seq(1,5,1),seq(6,10,1),seq(11,15,1))
我想找到每个站点每天几年的平均海冰
x=c(1,2,3) ## years
y=c(1:5) ## stations
…并以与上述三个列表中任何一个相同的格式将其存储在新列表中
差不多
[[day]][station]....[n station]
.
.
.
[[n day]][station]....[n station]
average.ice =rep( list(rep(NA, length(y))), 3 )
foreach(x=x) %do% {
foreach(y=y) %do% {
average.ice[[x]][y] = mean(c(ice_1980[[x]][y],ice_1990[[x]][y],ice_2000[[x]][y]))
}
}
我试过类似的东西
[[day]][station]....[n station]
.
.
.
[[n day]][station]....[n station]
average.ice =rep( list(rep(NA, length(y))), 3 )
foreach(x=x) %do% {
foreach(y=y) %do% {
average.ice[[x]][y] = mean(c(ice_1980[[x]][y],ice_1990[[x]][y],ice_2000[[x]][y]))
}
}
但我的输出中有NAs
average.ice
[[1]]
[1] 1 2 3
[[2]]
[1] NA NA 8
[[3]]
[1] NA NA 13
我哪里做错了?在apply系列中有没有更聪明的方法?请注意索引。这就是你想要的吗
yy=c(1,2,3) ## years
st=c(1:5) ## stations
average.ice =rep( list(rep(NA, length(st))), 3 )
library(foreach)
foreach(x=yy) %do% {
foreach(y=st) %do% {
average.ice[[x]][y] = mean(c(ice_1980[[x]][y],ice_1990[[x]][y],ice_2000[[x]][y]))
}
}
您可能正在寻找以下内容:
numOfStations <- length(ice_1980)
average.ice <- lapply(1:numOfStations,
function(i) mapply(mean, ice_1980[[i]], ice_1990[[i]], ice_2000[[i]]))
numOfStations