如何在一个列表中的矩阵上使用cbind并将其放置在另一个列表中(R) [英] How to use cbind on matrices in one list and place them in another (r)
问题描述
我正试图合并存储在嵌套列表中的矩阵,并将它们放置在新列表中.例如,如果我有一个水果列表,我想获取存储在Kiwi
下的各种矩阵,并将它们作为一个矩阵合并到一个新列表中.
I'm trying to join matrices stored in nested lists and place them in a new list. For example, if I have a list of fruit, I would like to take various matrices stored under Kiwi
, and join them together as one matrix in a new list.
这将生成类似于我的数据的东西:
This will generate something that looks like my data:
#Define some important things
Fruits = c('Mango', 'Kiwi')
Attr = c('Size', 'Shape')
#generate empty lists
MyFruitList <- lapply(Fruits, function(q) {
EmptySublist <- (setNames(vector("list", length(Fruits)), Attr))
})
names(MyFruitList) <- Fruits
#Full lists with example matrices
MyFruitList[['Mango']][['Size']] <- matrix(c(3,5,7,2), nrow=2, ncol=2)
MyFruitList[['Mango']][['Shape']] <- matrix(c(3,6,7,5), nrow=2, ncol=2)
MyFruitList[['Kiwi']][['Size']] <- matrix(c(1,3,4,2), nrow=2, ncol=2)
MyFruitList[['Kiwi']][['Shape']] <- matrix(c(2,4,5,1), nrow=2, ncol=2)
这就是我一直试图将存储在猕猴桃和芒果下的矩阵移到新列表中的方法.
And here is what I have been trying to use to move the matrices stored under Kiwi and Mango into a new list.
#Obviously this doesn't actually work
MyFruitListAsRows <- lapply(Fruits, function(i) {
MyFruitListAsRows <- matrix(cbind(paste0(MyFruitList[i])))
})
names(MyFruitListAsRows) <- paste0(Fruits, "Row")
理想情况下,我应该得到一个名为MyFruitsAsRows
的列表,该列表包含2,4 x 2名为Kiwi
和Mango
的矩阵,其中包含来自原始MyFruitList
列表的各自的Size
和Shape
数据.
Ideally I should end up with a list called MyFruitsAsRows
which contains 2, 4 by 2 matrices named Kiwi
and Mango
, containing their respective Size
and Shape
data from the original MyFruitList
list.
例如对于芒果,它看起来像这样:
e.g. For Mango it would look like this:
[,1] [,2] [,3] [,4]
[1,] 3 7 3 7
[2,] 5 2 6 5
(很抱歉,这些数字过于相似,计划不周,一开始可能很难识别我希望我的数字去哪里)
(Sorry that the numbers are overly similar, that was not well planned and might make it hard at first to recognise where I'd like my numbers to go)
已从此构造:
$Size
[,1] [,2]
[1,] 3 7
[2,] 5 2
$Shape
[,1] [,2]
[1,] 3 7
[2,] 6 5
我已经尝试适应罗纳克·沙阿(Ronak Shah)的建议,并执行以下操作:
I have tried to adapt the advice of Ronak Shah and done the following:
library(tidyverse)
MyFruitListAsRows <- map(MyFruitList[i], bind_cols)
但同时运行,
MyFruitListAsRows[['KiwiRow']]
MyFruitListAsRows[['MangoRow']]
产生:
I get Error in x[i, , drop = FALSE] : subscript out of bounds
如果我试图让RStudio在窗口中向我显示两个新列表中的内容,RStudio会遇到致命错误并崩溃.
If I try to get RStudio to show me what's in either of my new lists in a window, RStudio encounters a fatal error and crashes.
推荐答案
我们可以使用基数R遍历每个MyFruitList
和cbind
与do.call
We can use base R to loop over each MyFruitList
and cbind
them with do.call
lapply(MyFruitList, function(x) do.call(cbind, x))
#$Mango
# [,1] [,2] [,3] [,4]
#[1,] 3 7 3 7
#[2,] 5 2 6 5
#$Kiwi
# [,1] [,2] [,3] [,4]
#[1,] 1 4 2 5
#[2,] 3 2 4 1
您也可以在此处使用cbind.data.frame
.
使用tidyverse
我们可以在每个列表上依次map
,然后在cbind
Using tidyverse
we can map
over each list and then cbind
library(tidyverse)
map(MyFruitList, cbind.data.frame)
#$Mango
# Size.1 Size.2 Shape.1 Shape.2
#1 3 7 3 7
#2 5 2 6 5
#$Kiwi
# Size.1 Size.2 Shape.1 Shape.2
#1 1 4 2 5
#2 3 2 4 1
这篇关于如何在一个列表中的矩阵上使用cbind并将其放置在另一个列表中(R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!