markfairbanks / tidytable

Tidy interface to 'data.table'
https://markfairbanks.github.io/tidytable/
Other
449 stars 33 forks source link

Bug: unnest() #783

Closed PathosEthosLogos closed 9 months ago

PathosEthosLogos commented 9 months ago

Discovered through https://community.rstudio.com/t/a-chr-column-has-vectors-and-atomic-values-how-can-i-turn-those-vectors-into-longer-pivoted-rows/177823

library(tidyverse) |> suppressPackageStartupMessages()

df_example <- tibble(a = list(c('a','b'),
                                 character(0),
                                 "z",
                                 c('y','z')),
                        id = list('aa', 'bb', 'cc', 'cc'))

df_example |>
  unnest(cols = a,keep_empty = TRUE)
#> # A tibble: 6 × 2
#>   a     id       
#>   <chr> <list>   
#> 1 a     <chr [1]>
#> 2 b     <chr [1]>
#> 3 <NA>  <chr [1]>
#> 4 z     <chr [1]>
#> 5 y     <chr [1]>
#> 6 z     <chr [1]>
library(tidytable) |> suppressPackageStartupMessages()

df_example <- tidytable(a = list(c('a','b'),
                                 character(0),
                                 "z",
                                 c('y','z')),
                        id = list('aa', 'bb', 'cc', 'cc'))

df_example |>
  unnest(a, keep_empty = TRUE)
#> # A tidytable: 5 × 1
#>   a    
#>   <chr>
#> 1 a    
#> 2 b    
#> 3 z    
#> 4 y    
#> 5 z

Created on 2023-11-28 with reprex v2.0.2

markfairbanks commented 9 months ago

Hmm it looks like a vector of length 0 should be kept when keep_empty = TRUE (currently it only works on NULL elements)

markfairbanks commented 9 months ago

All set - thanks for catching this

library(tidytable) |> suppressPackageStartupMessages()

df_example <- tidytable(a = list(c('a','b'),
                                 character(0),
                                 "z",
                                 c('y','z')),
                        id = list('aa', 'bb', 'cc', 'cc'))

df_example |>
  unnest(a, keep_empty = TRUE)
#> # A tidytable: 6 × 1
#>   a    
#>   <chr>
#> 1 a    
#> 2 b    
#> 3 <NA> 
#> 4 z    
#> 5 y    
#> 6 z