【问题标题】:Best way to covert List to Matrix or Tibble format?将 List 转换为 Matrix 或 Tibble 格式的最佳方法?
【发布时间】:2020-11-23 19:37:51
【问题描述】:

我正在寻找一种体面的方法将函数的输出作为列表转换为矩阵或 tibble 格式。

以下 tibble 输入到一个函数中。该函数返回一个列表。在这个简单的示例中,返回的列表恰好包含与函数输入 tibble 相同的值。

# # A tibble: 6 x 15
#         rev   CoS    gm   sga ebitda    bd  ebit    ie    ii  gain   ebt chg_DTL   current   tax    ni
#       <dbl> <dbl> <dbl> <dbl>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>   <dbl>     <dbl> <dbl> <dbl>
#     1     0     0     0     0      0     0     0     0     0     0     0       0         0     0     0
#     2     0     0     0     0      0     0     0     0     0     0     0       0         0     0     0
#     3     0     0     0     0      0     0     0     0     0     0     0       0         0     0     0
#     4     0     0     0     0      0     0     0     0     0     0     0       0         0     0     0
#     5     0     0     0     0      0     0     0     0     0     0     0       0         0     0     0
#     6     0     0     0     0      0     0     0     0     0     0     0       0         0     0     0

这是从函数返回的列表。

> ni_out
$rev
[1] 0 0 0 0 0 0

$CoS
[1] 0 0 0 0 0 0

$gm
[1] 0 0 0 0 0 0

$sga
[1] 0 0 0 0 0 0

$ebitda
[1] 0 0 0 0 0 0

$bd
[1] 0 0 0 0 0 0

$ebit
[1] 0 0 0 0 0 0

$ie
[1] 0 0 0 0 0 0

$ii
[1] 0 0 0 0 0 0

$gain
[1] 0 0 0 0 0 0

$ebt
[1] 0 0 0 0 0 0

$chg_DTL_net
[1] 0 0 0 0 0 0

$current
[1] 0 0 0 0 0 0

$tax
[1] 0 0 0 0 0 0

$ni
[1] 0 0 0 0 0 0


我希望将其转换回更令人愉悦的内容,例如原始 tibble 格式或矩阵。

我获得了列表输出的维度。

lengths(ni_out)[[1]]
# [1] 6

> length(ni_out)
# [1] 15

但是,我对矩阵的不成功尝试如下所示。

as.matrix(unlist(ni_out), nrow = lengths(ni_out)[[1]], ncol = length(ni_out))
# [,1]
# rev1            0
# rev2            0
# rev3            0
# rev4            0
# rev5            0
# rev6            0
# CoS1            0
# CoS2            0
# CoS3            0
# CoS4            0
# CoS5            0
# CoS6            0
# gm1             0
# gm2             0
# gm3             0
# gm4             0
# gm5             0
# gm6             0
# sga1            0
# sga2            0
# sga3            0
# sga4            0
# sga5            0
# sga6            0
# ebitda1         0
# ebitda2         0
#         etc.

对矩阵或 tibble 格式的想法??

【问题讨论】:

  • 假设您的列表列表被称为 l... 尝试 df &lt;- data.frame(matrix(unlist(l), nrow=length(l), byrow=T))

标签: r


【解决方案1】:

下次请提供可重现的例子。

如果您的列表名为mylist,我会尝试data.table::rbindlist(mylist)

请参阅下面的示例,包括将向量转换为data.frames

dat <- 0:5
mylist <- list(dat, dat, dat)
mylist <- lapply(mylist, function(x) data.frame(t(x)))
data.table::rbindlist(mylist)
> data.table::rbindlist(mylist)
X1 X2 X3 X4 X5 X6
1:  0  1  2  3  4  5
2:  0  1  2  3  4  5
3:  0  1  2  3  4  5

编辑:您似乎想要cbind 而不是rbind,所以在这种情况下我会使用以下内容。

dat <- 0:5
mylist <- list(dat, dat, dat)
mylist <- lapply(mylist, function(x) data.frame(x))
dplyr::bind_cols(mylist)
x...1 x...2 x...3
1     0     0     0
2     1     1     1
3     2     2     2
4     3     3     3
5     4     4     4
6     5     5     5

如您所见,答案因您的需求而异,因此提供示例很重要。

【讨论】:

  • data.frame(matrix(unlist(ni_out), nrow=lengths(ni_out), byrow=T)) 那行得通,只需要使用“长度”而不是“长度”来保持正确#行,# 列
  • @123blee 检查我的答案你不需要设置长度或行数
【解决方案2】:

你可以像这样使用do.call函数:

a <- list(data.frame(x=1:5),data.frame(y=1:5))
do.call("cbind",a)

也检查cbindlist函数。

【讨论】:

    【解决方案3】:

    只需拨打data.frameas_tibble就可以了:

    l <- list(x=rep(0,6),y=rep(0,6), z=rep(0,6), t=rep(0,6))
    data.frame(l)
      x y z t
    1 0 0 0 0
    2 0 0 0 0
    3 0 0 0 0
    4 0 0 0 0
    5 0 0 0 0
    6 0 0 0 0
    as_tibble(l)
    # A tibble: 6 x 4
          x     y     z     t
      <dbl> <dbl> <dbl> <dbl>
    1     0     0     0     0
    2     0     0     0     0
    3     0     0     0     0
    4     0     0     0     0
    5     0     0     0     0
    6     0     0     0     0
    

    至于矩阵,先将其转换为 data.frame,然后再转换为矩阵

    as.matrix(data.frame(l))
         x y z t
    [1,] 0 0 0 0
    [2,] 0 0 0 0
    [3,] 0 0 0 0
    [4,] 0 0 0 0
    [5,] 0 0 0 0
    [6,] 0 0 0 0
    

    【讨论】:

      【解决方案4】:

      as.data.table 的另一个选项

      library(data.table)
      as.data.table(l)
      

      数据

      l <- list(x=rep(0,6),y=rep(0,6), z=rep(0,6), t=rep(0,6))
      

      【讨论】:

        猜你喜欢
        • 2011-07-02
        • 1970-01-01
        • 2012-02-22
        • 1970-01-01
        • 2011-04-06
        • 2013-12-22
        • 1970-01-01
        • 1970-01-01
        • 2023-04-11
        相关资源
        最近更新 更多