【问题标题】:How to use cbind on matrices in one list and place them in another (r)如何在一个列表中的矩阵上使用 cbind 并将它们放在另一个列表中(r)
【发布时间】:2019-04-21 14:06:43
【问题描述】:

我正在尝试加入存储在嵌套列表中的矩阵并将它们放入一个新列表中。例如,如果我有一个水果列表,我想获取存储在Kiwi 下的各种矩阵,并将它们连接在一起作为一个新列表中的一个矩阵。

这将生成一些看起来像我的数据的东西:

#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)

这是我一直在尝试使用的将存储在 Kiwi 和 Mango 下的矩阵移动到新列表中的方法。

#Obviously this doesn't actually work
MyFruitListAsRows <- lapply(Fruits, function(i) {
  MyFruitListAsRows <- matrix(cbind(paste0(MyFruitList[i]))) 
})
names(MyFruitListAsRows) <- paste0(Fruits, "Row")

理想情况下,我应该最终得到一个名为 MyFruitsAsRows 的列表,其中包含名为 KiwiMango 的 2、4 x 2 矩阵,分别包含来自原始 MyFruitList 列表的 SizeShape 数据.

例如对于 Mango,它看起来像这样:

     [,1] [,2] [,3] [,4] 
[1,]    3    7   3    7
[2,]    5    2   6    5

(抱歉,数字过于相似,计划不周,一开始可能很难认出我希望我的数字去哪里)

由此构建:

$Size
     [,1] [,2]
[1,]    3    7
[2,]    5    2

$Shape
     [,1] [,2]
[1,]    3    7
[2,]    6    5

编辑:我尝试采纳 Ronak Shah 的建议并做了以下工作:

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 会遇到致命错误并崩溃。

【问题讨论】:

    标签: r list matrix cbind


    【解决方案1】:

    我们可以使用 base R 循环遍历每个 MyFruitListcbinddo.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

    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
    

    【讨论】:

    • 有没有办法让数据保存在 4*2 配置中?其中我有 2 行 4 列,第 1 列和第 2 列包含第一个数据集(例如 Size 矩阵),第 3 列和第 4 列包含第二个数据集(例如 Shape 矩阵)?我试图看看bind_columns 产生了什么,但这似乎不是告诉整洁者我想要做什么的正确方式。非常感谢您的帮助。
    • 我添加了一个更好的示例来说明我希望如何布置最终数据。如果你有机会看看,我将非常感激。我认为 4*2 首先指的是 x 轴,然后是 y 轴,但事实并非如此,因为您的 4 * 2 tibble (# A tibble: 4 x 2) 与我尝试的相反实现。再次感谢您的帮助,特别是因为这不是您第一次帮助我。
    • 我发现bind_cols 在某种程度上有效。但是,当我尝试使用MyFruitListAsRows[['KiwiRow']]MyFruitListAsRows[['MangoRow']] 检查我的矩阵时,我得到Error in x[i, , drop = FALSE] : subscript out of bounds。当我尝试在 RStudio 窗口中查看它时,我得到R session aborted / R encountered a fatal error / The session was terminated。很抱歉将上述传奇留在评论部分。
    • @SolebaySharp 我已经相应地更新了答案。看看它是否适合你。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2019-01-27
    • 1970-01-01
    • 2018-11-22
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多