从矩阵列表中查找矩阵 [英] rbind matrices from a list of a list of matrices

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

问题描述

我有一个矩阵的一个list的一个list.每个list具有相同数量的matrices,其中每个matrix具有相同数量的列:

I have a list of a list of matrices. Each list has the same number of matrices where each matrix has the same number of columns:

set.seed(1)

mat.lol <- list(list1=list(matrix(rnorm(100),ncol=10),matrix(rnorm(200),ncol=10),matrix(rnorm(140),ncol=10)),
                list2=list(matrix(rnorm(80),ncol=10),matrix(rnorm(220),ncol=10),matrix(rnorm(110),ncol=10)),
                list3=list(matrix(rnorm(300),ncol=10),matrix(rnorm(500),ncol=10),matrix(rnorm(650),ncol=10)))

而且我想在所有列表中分别rbind每个matrix i,以便最终得到matriceslist:

And I'd like to rbind each matrix i across all lists so that I end up with this list of matrices:

mat.list <- list(rbind(mat.lol[[1]][[1]],mat.lol[[2]][[1]],mat.lol[[3]][[1]]),
                 rbind(mat.lol[[1]][[2]],mat.lol[[2]][[2]],mat.lol[[3]][[2]]),
                 rbind(mat.lol[[1]][[3]],mat.lol[[2]][[3]],mat.lol[[3]][[3]]))

达到目标的apply function是什么?

推荐答案

您可以使用purrr包中的transpose()函数将列表内外翻出 ,以便每个子列表都包含所有您想要绑定在一起的矩阵,然后可以简单地循环遍历结果列表和rbind矩阵:

You can use transpose() function from purrr package to turn the list inside out so that each sub list contains all the matrices you want to bind together, and then you can simply loop through the result list and rbind the matrices:

library(purrr)
mat.list.1 <- lapply(transpose(mat.lol), do.call, what=rbind)

identical(mat.list, mat.list.1)
# [1] TRUE


坚持使用purrr语法:


To stick with purrr syntax:

mat.list.3 <- transpose(mat.lol) %>% map(do.call, what=rbind)

identical(mat.list, mat.list.3)
# [1] TRUE


或者您可以从R底下使用Map:

mat.list.2 <- do.call(Map, c(f = rbind, mat.lol))

identical(mat.list, mat.list.2)
# [1] TRUE

这篇关于从矩阵列表中查找矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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