简体   繁体   English

取消嵌套多个列

[英]Unnest multiple columns

I have this type of data with multiple list columns:我有这种类型的数据,其中包含多个列表列:

df <- structure(list(go = c("go after it", "here we go", "he went bust", 
                            "go get it go", "i 'm gonna go", "she 's going berserk"), left = list(
                              "", "we", "he", c("", "it"), c("'m", "gonna"), "'s"), node = list(
                                "go", "go", "went", c("go", "go"), c("gonna", "go"), "going"), 
                     right = list("after", "", "bust", c("get", ""), c("go", ""
                     ), "berserk")), class = "data.frame", row.names = c(NA, -6L
                     ))

My aim is unlist the three columns left , node , and right .我的目标是取消列出leftnoderight三列。 Both unnest_longer and unnest_auto seem to be a step in the right direction but I'm unsure how to proceed from there: unnest_longerunnest_auto似乎都是朝着正确方向迈出的一步,但我不确定如何从那里开始:

library(tidyr)
df %>%
  unnest_longer(node) # or:   unnest_auto(node)
# A tibble: 8 × 4
  go                   left      node   right    
  <chr>                <list>    <chr>  <list>   
1 go after it          <chr [0]> go     <chr [1]>
2 here we go           <chr [1]> go     <chr [0]>
3 he went bust         <chr [1]> went   <chr [1]>
4 go get it go         <chr [1]> go     <chr [1]>
5 go get it go         <chr [1]> go     <chr [1]>
6 i 'm gon na go       <chr [2]> gon na <chr [1]>
7 i 'm gon na go       <chr [2]> go     <chr [1]>
8 she 's going berserk <chr [1]> going  <chr [1]>

The expected outcome is this:预期的结果是这样的:

# go                         left      node     right  
# <chr>                      <chr>    <chr>     <chr>  
# 1 go after it                         go       after  
# 2 here we go                 we       go       
# 3 he went bust               he       went     bust   
# 4 go get it go                        go       get    
# 5 go get it go               it       go           
# 6 i 'm gon na go             'm       gon na   go     
# 7 i 'm gon na go         gon na       go            
# 8 she 's going berserk       's       going    berserk 

We may use where我们可以使用where

library(dplyr)
library(tidyr)
df %>%
    unnest(where(is.list), keep_empty = TRUE)
library(tidyverse)

df %>% unnest(c(left, node, right), keep_empty = TRUE)
#> # A tibble: 8 × 4
#>   go                   left    node  right    
#>   <chr>                <chr>   <chr> <chr>    
#> 1 go after it          ""      go    "after"  
#> 2 here we go           "we"    go    ""       
#> 3 he went bust         "he"    went  "bust"   
#> 4 go get it go         ""      go    "get"    
#> 5 go get it go         "it"    go    ""       
#> 6 i 'm gonna go        "'m"    gonna "go"     
#> 7 i 'm gonna go        "gonna" go    ""       
#> 8 she 's going berserk "'s"    going "berserk"

Created on 2021-12-04 by the reprex package (v2.0.1)reprex package (v2.0.1) 于 2021 年 12 月 4 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM