[英]how to calculate the mean of list of list of matrices in r
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). 我有一个如下所示的列表,它是包含矩阵的列表列表(因此“ftable”是十个列表的列表,每个内部列表包含七个矩阵)。 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.
我需要计算相关矩阵的平均值,这些矩阵也可能有NA值。我尝试了几种方法,但是我遇到了错误。
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
.......
.......
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. 因此,我需要有一个包含七个矩阵的列表,每个矩阵都是将均值应用于ftable [[1]] [[i]],ftable [[2]] [[i]],... 。,ftable [[10]] [[i]]和我在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
one example of the matrices: 矩阵的一个例子:
> 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
I think this is what you require. 我认为这就是你所需要的。 The easiest way would be to unlist your outer list and then apply
Reduce
as follows: 最简单的方法是取消列出您的外部列表,然后按如下方式应用
Reduce
:
I'll create a variation of the input from user1317221_G
我将从
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
Now the solution. 现在的解决方案。
# 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
you mean like this: 你的意思是这样的:
mat1 <- matrix(c(1:10,NA,NA),nrow=2)
matlist1 <- list(mat1,mat1,mat1)
bigmatlist <- list(matlist1,matlist1)
mean(mat1, na.rm=TRUE)
#[1] 5.5
sapply(matlist1, function(x) mean(x,na.rm=TRUE))
#[1] 5.5 5.5 5.5
and a list of lists: 和列表清单:
sapply(bigmatlist,function(x) sapply(x, function(x) mean(x,na.rm=TRUE)) )
# [,1] [,2]
#[1,] 5.5 5.5
#[2,] 5.5 5.5
#[3,] 5.5 5.5
changing the sapply
for lapply
where appropriate if you want lists returned. 如果要返回列表,
lapply
在适当的位置更改sapply
的lapply
。
where [3,][,1]
is the mean for matrix three in list 1 ie bigmatlist[[1]][[3]]
其中
[3,][,1]
是列表1中矩阵3的均值,即bigmatlist[[1]][[3]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.