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

如何计算r中矩阵表的平均值

如何计算r中矩阵表的平均值,r,matrix,mean,R,Matrix,Mean,我有一个如下的列表,这是一个包含矩阵的列表列表(因此“ftable”是一个包含十个列表的列表,每个内部列表包含七个矩阵)。我需要计算关联矩阵的平均值,这些矩阵也可能有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 ....... ...

我有一个如下的列表,这是一个包含矩阵的列表列表(因此“ftable”是一个包含十个列表的列表,每个内部列表包含七个矩阵)。我需要计算关联矩阵的平均值,这些矩阵也可能有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
.......
.......
你的意思是这样的:

 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
如果希望返回列表,请在适当的位置更改
lappy
sapply

其中,
[3,][,1]
是列表1中矩阵3的平均值,即
bigmatlist[[1]][[3]]
您的意思是:

 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
如果希望返回列表,请在适当的位置更改
lappy
sapply


其中,
[3,][,1]
是列表1中矩阵3的平均值,即
bigmatlist[[1]][[3]]

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

我将从
user1317221\u 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
set.seed(45)

mat1我想这就是你需要的。最简单的方法是取消外部列表的列表,然后应用
Reduce
,如下所示:

我将从
user1317221\u 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
set.seed(45)

mat1非常感谢Arun,这正是我的意思,尽管我不明白它是如何工作的。我不明白您为什么取消列出它,以及“Reduce(+',list.un[seq(idx,prod.len,by=inner.len)]”是如何工作的:(事实上,它帮助了很多。我总是需要处理大量数据,比如列表列表和如此多的多层数据,有时我会混淆如何管理。我正在对一些不同的方法进行一些统计测试,对于每种方法,我总是应该有许多随机样本,对于每个样本,有许多子集,这迫使我有这个ki数据结构的nds。再次非常感谢!非常感谢Arun这正是我的意思,尽管我不明白它是如何工作的。我不明白你为什么取消列出它,以及“Reduce(+',list.un[seq(idx,prod.len,by=inner.len)]”是如何工作的:(事实上,它帮助了很多。我总是需要处理大量数据,比如列表列表和如此多的多层数据,有时我会混淆如何管理。我正在对一些不同的方法进行一些统计测试,对于每种方法,我总是应该有许多随机样本,对于每个样本,有许多子集,这迫使我有这个ki数据结构的nds。再次感谢!
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