在 R 的数据框中取消嵌套向量列表

Unnest a list of vectors in a data frame in R

我有以下数据框:

library(dplyr)

tibble(
  x = 1:5000,
  y = rnorm(5000),
  z = list(seq(1, 100, 10))
)
#> # A tibble: 5,000 x 3
#>        x       y z         
#>    <int>   <dbl> <list>    
#>  1     1 -0.0973 <dbl [10]>
#>  2     2 -1.65   <dbl [10]>
#>  3     3 -0.636  <dbl [10]>
#>  4     4 -1.33   <dbl [10]>
#>  5     5 -0.177  <dbl [10]>
#>  6     6 -0.271  <dbl [10]>
#>  7     7  0.506  <dbl [10]>
#>  8     8 -1.07   <dbl [10]>
#>  9     9 -1.28   <dbl [10]>
#> 10    10 -1.31   <dbl [10]>
#> # … with 4,990 more rows

其中column z是一个向量,例如:

seq(1, 100, 10)
#>  [1]  1 11 21 31 41 51 61 71 81 91

并且这些向量元素中的每一个都应该是一列。因此,这是我的预期输出(请注意,我不关心列的名称):

#> # A tibble: 5,000 x 12
#>        x      y  ...1  ...2  ...3  ...4  ...5  ...6  ...7  ...8  ...9 ...10
#>    <int>  <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1     1  1.62      1    11    21    31    41    51    61    71    81    91
#>  2     2  1.45      1    11    21    31    41    51    61    71    81    91
#>  3     3 -1.61      1    11    21    31    41    51    61    71    81    91
#>  4     4  1.09      1    11    21    31    41    51    61    71    81    91
#>  5     5  3.16      1    11    21    31    41    51    61    71    81    91
#>  6     6  0.313     1    11    21    31    41    51    61    71    81    91
#>  7     7 -1.11      1    11    21    31    41    51    61    71    81    91
#>  8     8  1.50      1    11    21    31    41    51    61    71    81    91
#>  9     9 -1.01      1    11    21    31    41    51    61    71    81    91
#> 10    10  0.149     1    11    21    31    41    51    61    71    81    91
#> # … with 4,990 more rows

我可以使用 tidyr::unnest_wider():

实现上述目标
library(dplyr)
library(tidyr)

tibble(
  x = 1:5000,
  y = rnorm(5000),
  z = list(seq(1, 100, 10))
) %>% 
  unnest_wider(col = z)

但问题是这对于大数据帧来说相当慢。我想知道是否有另一种方法可以使用更快的函数实现相同的目标?

所以,我认为这比 unnest_wider 快得多,也可以有其他选择。

library(tidyverse)
d <- data %>% 
     cbind(., do.call('rbind', .$z)) %>% 
     select(-z)