如何在R中堆叠多个矩阵 [英] How to stack multiple matrices in R

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

问题描述

我有一个矩阵列表,类似于下面的代码获得的列表:

I have a list of matrices, similar to the one obtained by the code below:

a <- matrix(1, ncol=2, nrow=3)
b <- matrix(2, ncol=2, nrow=3)
c <- matrix(3, ncol=2, nrow=3)
d <- list(a, b, c)

我想将它们堆叠起来,以便它们位于一个矩阵中,类似于这一矩阵:

I want to stack them so that they are in one matrix, similar to this one:

e <- rbind(d[[1]], d[[2]], d[[3]])

诀窍是我事先不知道需要连接多少个矩阵.有没有好的方法可以编写将所有矩阵堆叠在列表中的代码?

The trick is that I don't know in advance how many matrices will need to be joined. Is there a good way to write code that will stack all matrices in the list?

推荐答案

经典do.call:

     do.call(rbind,d)

使用data.table软件包的另一种选择:

Another option using data.table package:

library(data.table)
rbindlist(lapply(d,as.data.frame))

这篇关于如何在R中堆叠多个矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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