Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中列表中索引元素的平均值_R_List_Mean - Fatal编程技术网

R中列表中索引元素的平均值

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)) 我想找到每个站点每天几年的平均

我有一个与本文中提出的问题类似的问题,但是我对获取列表中的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))
我想找到每个站点每天几年的平均海冰

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