简体   繁体   English

合并R中的两个列表并保留列表中的结构

[英]merge two list in R and preserve structure in the list

I have two list with multiple elements in R . 我在R有两个包含多个元素的列表。 Eaach element is in matrix format Eaach元素为矩阵格式

list A has 10 rows 5 columns and list B has 5 rows and 5 columns, I would like to append each element in List B to list A. I would like to preserve the matrix structure in each element of the list. 列表A有10行5列,列表B有5行5列,我想将列表B中的每个元素追加到列表A。我想在列表的每个元素中保留矩阵结构。 The newappended list would have 15 rows and 5 columns. 新追加的列表将具有15行和5列。 Bascially I'm appending matrices from each list together. 基本上,我会将每个列表中的矩阵加在一起。

As an example: 举个例子:

listA <- list(x=matrix(rnorm(50), nrow=10),
              y=matrix(rnorm(50), nrow=10))
listB <- list(x=matrix(rnorm(25), nrow=5),
              y=matrix(rnorm(25), nrow=5))

I tried few solutions in this site but both of them give vector and doe not preserve mXn structure. 我在此站点尝试了几种解决方案,但它们都给出了向量,并且没有保留mXn结构。

k <- mapply(c,listA,listB,simplify=FALSE)

t <- apply(cbind(listA, listB),2,function(x) unname(unlist(x))

Any help would be greatly appreciated. 任何帮助将不胜感激。

)

Try 尝试

 listAB <- Map(`rbind`, listA, listB)
 sapply(listAB, dim)
 #     x  y
 #[1,] 15 15
 #[2,]  5  5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM