如何计算R中矩阵列表的平均值 [英] how to calculate the mean of list of list of matrices in r

查看:1478
本文介绍了如何计算R中矩阵列表的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于以下的列表,它是包含矩阵的列表的列表(因此"ftable"是十个列表的列表,每个内部列表都包含七个矩阵).我需要计算可能也具有NA值的关联矩阵的均值.我尝试了几种方法,但都出错了.

I have a list like below, which is a list of lists containing matrices(so "ftable" is a list of ten lists and each of the internal lists contains seven matrices). I need to calculate the mean of associated matrices which may also have NA values as well.I have tried several ways but I got errors.

for(i in 1:10){
for(j in 1:7){
ftable[[i]][[j]] <- matrix (x,nrow=8,ncol=8, byrow=TRUE)
}
}

> str(ftable)
List of 10
$ :List of 7
.......
.......

作为结果,我需要有一个包含七个矩阵的列表,这些矩阵中的每一个都是对ftable [[1]] [[i]],ftable [[2]] [[i]]应用均值的结果,...,ftable [[10]] [[i]]和i在1:7中.

as the result I need to have a list containing seven matrices that each of these matrices are the result of applying mean to ftable[[1]][[i]], ftable[[2]][[i]], ... , ftable[[10]][[i]] and i in 1:7.

我已经尝试过,但是出现错误:

I have tried this but I got error:

meanTable <- list()
for (i in 1:7)
meanTable[[i]] <- matrix (0, nrow=8,ncol=8)

> meanTable <- lapply(1:7, function(i) Reduce(mean, list(ftable[[1]][i],ftable[[2]][i],ftable[[3]][i],ftable[[4]][i],ftable[[5]][i],ftable[[6]][i],ftable[[7]][i],ftable[[8]][i],ftable[[9]][i],ftable[[10]][i])))
Error in mean.default(init, x[[i]]) : 
'trim' must be numeric of length one
In addition: Warning message:
In mean.default(init, x[[i]]) :
argument is not numeric or logical: returning NA

矩阵的一个示例:

> ftable[[1]][[1]]
1     2     3      4      5      6      7      8
1 NA 0.924 0.835 -0.336  0.335 -0.948  0.285  0.749
2 NA    NA 0.772 -0.333  0.333 -0.892  0.127  0.715
3 NA    NA    NA -0.476  0.475 -0.756  0.258  0.749
4 NA    NA    NA     NA -0.999  0.399 -0.150 -0.399
5 NA    NA    NA     NA     NA -0.399  0.151  0.399
6 NA    NA    NA     NA     NA     NA -0.134 -0.715
7 NA    NA    NA     NA     NA     NA     NA  0.144
8 NA    NA    NA     NA     NA     NA     NA     NA

推荐答案

我认为这是您所需要的.最简单的方法是取消列出外部列表,然后按如下所示应用Reduce:

I think this is what you require. The easiest way would be to unlist your outer list and then apply Reduce as follows:

我将创建来自user1317221_G

set.seed(45)
mat1 <- matrix(c(sample(10),NA,NA),nrow=2)
matlist1 <- list(mat1,mat1,mat1)
mat2 <- matrix(c(sample(11:20),NA,NA),nrow=2)
matlist2 <- list(mat2,mat2,mat2)
bigmatlist <- list(matlist1,matlist2)

> bigmatlist
# [[1]]
# [[1]][[1]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    7    2   10    1    4   NA
# [2,]    3    9    8    5    6   NA
# 
# [[1]][[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    7    2   10    1    4   NA
# [2,]    3    9    8    5    6   NA
# 
# [[1]][[3]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    7    2   10    1    4   NA
# [2,]    3    9    8    5    6   NA
# 
# 
# [[2]]
# [[2]][[1]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   14   13   19   17   15   NA
# [2,]   18   20   16   11   12   NA
# 
# [[2]][[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   14   13   19   17   15   NA
# [2,]   18   20   16   11   12   NA
# 
# [[2]][[3]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]   14   13   19   17   15   NA
# [2,]   18   20   16   11   12   NA

现在解决.

# in your case, outer.len = 10 and inner.len = 7
outer.len <- 2
inner.len <- 3
prod.len <- outer.len * inner.len
list.un <- unlist(bigmatlist, recursive = FALSE)
o <- lapply(1:inner.len, function(idx) {
    Reduce('+', list.un[seq(idx, prod.len, by = inner.len)])/outer.len
})

> o
# [[1]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 10.5  7.5 14.5    9  9.5   NA
# [2,] 10.5 14.5 12.0    8  9.0   NA
# 
# [[2]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 10.5  7.5 14.5    9  9.5   NA
# [2,] 10.5 14.5 12.0    8  9.0   NA
# 
# [[3]]
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 10.5  7.5 14.5    9  9.5   NA
# [2,] 10.5 14.5 12.0    8  9.0   NA

这篇关于如何计算R中矩阵列表的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆