如何将 tibble 列中的向量提取到同一 tibble 中的多个列?

How to extract a vector in a tibble column to multiple columns in the same tibble?

下面的代码在最后一列中生成一个长度为 6 的向量,我想从该列中提取 6 个新列到我的标题中。

require(tidyverse)
require(purrrlyr)
# this function will return a vector of the same length (6) for each group
fun=function(X,Y){
   mycut<-cut(X,breaks=seq(50,350,by=50),right=FALSE)
   v<-tapply(Y,mycut,sum)
   return(v)
}
# use the previous function to count gears per class of hp
mtcars %>%
    group_by(cyl)  %>%   
    by_slice(~fun(.x$hp,.x$gear)) %>%
    rename(cut=.out)

我的列中有一个向量 cut

# tibble [3 x 2]
     cyl       cut
  <fctr>    <list>
1      4 <dbl [6]>
2      6 <dbl [6]>
3      8 <dbl [6]>

我需要从这个向量传递什么命令到 table 之类的?

cyl  [50,100) [100,150) [150,200) [200,250) [250,300) [300,350) 
   4     36         9        NA        NA        NA        NA 
   ...

unnest 不起作用。我必须使用 by_row 还是有更直接的答案?

我们需要获取 'cut' 变量的 names 作为新列,然后在 unnest 之后执行 spread 重塑为 'wide' 格式正在处理 list 个元素

mtcars %>%
   group_by(cyl)  %>%   
   by_slice(~fun(.x$hp,.x$gear)) %>%
   rename(cut=.out) %>%
   mutate(Names = map(cut, ~factor(names(.x), levels = names(.x)))) %>%
   unnest %>%
   spread(Names, cut)
# A tibble: 3 x 7
#    cyl `[50,100)` `[100,150)` `[150,200)` `[200,250)` `[250,300)` `[300,350)`
#*  <dbl>      <dbl>       <dbl>       <dbl>       <dbl>       <dbl>       <dbl>
#1     4         36           9          NA          NA          NA          NA
#2     6         NA          22           5          NA          NA          NA
#3     8         NA          NA          21          15           5           5

我会建议另一种方法。您可以使用以下代码,而不是使用已弃用的 by_slice() 函数(现在位于 purrrlyr 包中):

mtcars %>% 
  split(.$cyl) %>% 
  map(~fun(.x$hp,.x$gear)) %>% 
  do.call(rbind, .)

给出以下输出

  [50,100) [100,150) [150,200) [200,250) [250,300) [300,350)
4       36         9        NA        NA        NA        NA
6       NA        22         5        NA        NA        NA
8       NA        NA        21        15         5         5