Home > database >  Unnest multiple columns
Unnest multiple columns

Time:12-05

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. Both unnest_longer and unnest_auto seem to be a step in the right direction but I'm unsure how to proceed from there:

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 

CodePudding user response:

We may use where

library(dplyr)
library(tidyr)
library(purrr)
df %>% 
   mutate(mx = invoke(pmax, across(where(is.list), lengths))) %>% 
   mutate(across(where(is.list), ~ map2(.x, mx, ~ {
        length(.x) <- .y
        if(cur_column() == "left") .x <- .x[order(!is.na(.x))]
        .x})), mx = NULL) %>%
   unnest(where(is.list))
# A tibble: 8 × 4
  go                   left  node   right  
  <chr>                <chr> <chr>  <chr>  
1 go after it          <NA>  go     after  
2 here we go           we    go     <NA>   
3 he went bust         he    went   bust   
4 go get it go         <NA>  go     get    
5 go get it go         it    go     <NA>   
6 i 'm gon na go       'm    gon na go     
7 i 'm gon na go       na    go     <NA>   
8 she 's going berserk 's    going  berserk

update

Based on the comments from OP, previous solution works

df %>%
    unnest(where(is.list))

If there are NULL elements, specify keep_empty = TRUE (in the OP's data, some of the elements were blank ("") instead of NULL, so the previous one should work as well

df %>%
     unnest(where(is.list), 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    "" 

CodePudding user response:

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)

  • Related