Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/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 - Fatal编程技术网

R 添加多个列表

R 添加多个列表,r,R,我的研究结果储存在列表中 我总共有1000个列表,它们都是相同的维度 每个列表包含39个元素,它们是不同维度的矩阵 我想将这些列表相加,然后除以1000 我想不出一个办法 例如: a<-matrix(0,nrow=5,ncol=6) b<-matrix(0,nrow=2,ncol=10) list1 <- list(a,b) a<-matrix(0,nrow=5,ncol=6) b<-matrix(0,nrow=2,ncol=10) list2 <- l

我的研究结果储存在列表中

我总共有1000个列表,它们都是相同的维度

每个列表包含39个元素,它们是不同维度的矩阵

我想将这些列表相加,然后除以1000

我想不出一个办法

例如:

a<-matrix(0,nrow=5,ncol=6)
b<-matrix(0,nrow=2,ncol=10)

list1 <- list(a,b)

a<-matrix(0,nrow=5,ncol=6)
b<-matrix(0,nrow=2,ncol=10)

list2 <- list(a,b)

a<-matrix(0,nrow=5,ncol=6)
b<-matrix(0,nrow=2,ncol=10)

list3 <- list(a,b)

a我最近为此实现了一些递归实用程序函数。但是,它们不检查前提条件(长度相等,元素的可和性)

编辑:我已经修复了评论中提到的问题。for循环被高阶函数取代,且该函数具有更好的错误行为。该函数还处理更复杂的列表结构,例如包含其他包含数字元素的列表。它比OP要求的更复杂,但我认为它值得保留,以防有人需要递归解决方案

sum_numeric_lists <- function(...){
  lists <- list(...)

  if (length(unique(sapply(lists, length))) > 1) {
    stop("lists are not of equal length")
  }

  Map(function(...) {
    elems <- list(...)
    if (length(unique(sapply(elems, class))) > 1) {
      stop("corresponding elements have different types")
    }
    if (is.list(elems[[1]])) {
      sum_numeric_lists(...)
    } else if(is.numeric(elems[[1]])){
      Reduce(`+`, elems)
    } else {
      warning("lists contain types other than numeric, which are preserved as NULL elements")
      NULL
    }
  }, ...)

}


devide_numeric_list_by <- function(l, divisor){

  lapply(X = l, FUN = function(elem) {
    if (is.list(elem)) {
      devide_numeric_list_by(elem, divisor)
    } else if(is.numeric(elem)){
      elem / divisor
    } else {
      warning("lists contain types other than numeric, which are preserved as NULL elements")
      NULL
    }
  })

}

avg_numeric_lists <- function(...){
  sum_l <- sum_numeric_lists(...)
  devide_numeric_list_by(sum_l, length(list(...)))
}

您可以组合使用
Map
Reduce
并使用
mget
将所有列表收集到一个列表中

a<-matrix(1,nrow=5,ncol=6)
b<-matrix(10,nrow=2,ncol=10)

list1 <- list(a,b)
list2 <- list(a,b)
list3 <- list(a,b)

l <- mget(ls(pattern = '^list\\d+$'))
(fl <- Reduce(function(x, y) Map(`+`, x, y), l))

# [[1]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    3    3    3    3    3    3
# [2,]    3    3    3    3    3    3
# [3,]    3    3    3    3    3    3
# [4,]    3    3    3    3    3    3
# [5,]    3    3    3    3    3    3
# 
# [[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]   30   30   30   30   30   30   30   30   30    30
# [2,]   30   30   30   30   30   30   30   30   30    30

Map(`/`, fl, 1000)

# [[1]]
#       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
# [1,] 0.003 0.003 0.003 0.003 0.003 0.003
# [2,] 0.003 0.003 0.003 0.003 0.003 0.003
# [3,] 0.003 0.003 0.003 0.003 0.003 0.003
# [4,] 0.003 0.003 0.003 0.003 0.003 0.003
# [5,] 0.003 0.003 0.003 0.003 0.003 0.003
# 
# [[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03  0.03
# [2,] 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03  0.03

a每个矩阵在每个列表中的位置都是相同的,因此我想将每个矩阵添加到同一个矩阵。列表中是否包含实际结果,以及单独的变量,如
list1
list2
。。。?或者它们存储在其他列表中?它们存储在单独的Rdata文件中。我正在努力摆脱R,所以我想提到的是,R有一个特定的语法来表示(否则无效)标识符,通过“
+
”(即带反勾),在这里使用字符串,虽然有效,是一个奇怪的黑客,如果R有一个合适的类型系统就不应该工作。这么说来,回答得很好。这是一种习惯,从打电话、图书馆等活动中延续下来。我来编辑一下。你投了反对票吗?在您指定列表来自1000个.RData文件之前,我写了这个答案。。。但是我想你会对另一个答案感到高兴的。我也没有否决这个,但它不是很好的代码。它很难读取,循环可能应该被适当的高阶列表函数所取代。此外,它还可以从正确的错误处理中获益,而不是将字符串写入结果中(很少有适合的情况)。
do.call(what = avg_numeric_lists, args = mget(ls(pattern = '^list\\d+$')))
a<-matrix(1,nrow=5,ncol=6)
b<-matrix(10,nrow=2,ncol=10)

list1 <- list(a,b)
list2 <- list(a,b)
list3 <- list(a,b)

l <- mget(ls(pattern = '^list\\d+$'))
(fl <- Reduce(function(x, y) Map(`+`, x, y), l))

# [[1]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    3    3    3    3    3    3
# [2,]    3    3    3    3    3    3
# [3,]    3    3    3    3    3    3
# [4,]    3    3    3    3    3    3
# [5,]    3    3    3    3    3    3
# 
# [[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,]   30   30   30   30   30   30   30   30   30    30
# [2,]   30   30   30   30   30   30   30   30   30    30

Map(`/`, fl, 1000)

# [[1]]
#       [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
# [1,] 0.003 0.003 0.003 0.003 0.003 0.003
# [2,] 0.003 0.003 0.003 0.003 0.003 0.003
# [3,] 0.003 0.003 0.003 0.003 0.003 0.003
# [4,] 0.003 0.003 0.003 0.003 0.003 0.003
# [5,] 0.003 0.003 0.003 0.003 0.003 0.003
# 
# [[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
# [1,] 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03  0.03
# [2,] 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03 0.03  0.03