-
Notifications
You must be signed in to change notification settings - Fork 417
Closed
Labels
featurea feature request or enhancementa feature request or enhancementrectangling 🗄️converting deeply nested lists into tidy data framesconverting deeply nested lists into tidy data frames
Milestone
Description
From https://community.rstudio.com/t/unnest-longer-drops-lists-rows-with-character-0/132748
Original example:
library(tidyverse)
my_df <- tibble(
txt = c(
"chestnut, pear, kiwi, peanut",
"grapes, banana"
)
)
#Extract all nuts
my_df <- my_df %>%
mutate(nuts = str_extract_all(txt, regex("\\w*nut\\w*"))) %>%
mutate(index = row_number(), .before=1)
#Row index 2 has nuts <chr [0]>
my_df
#> # A tibble: 2 × 3
#> index txt nuts
#> <int> <chr> <list>
#> 1 1 chestnut, pear, kiwi, peanut <chr [2]>
#> 2 2 grapes, banana <chr [0]>
#unnest
my_df_long <- my_df %>%
unnest_longer(nuts, values_to = "nuts_long")
#Row index 2 is now missing
my_df_long
#> # A tibble: 2 × 3
#> index txt nuts_long
#> <int> <chr> <chr>
#> 1 1 chestnut, pear, kiwi, peanut chestnut
#> 2 1 chestnut, pear, kiwi, peanut peanut
Created on 2022-03-28 by the reprex package (v2.0.1)
Minimal reprex:
library(tidyverse)
df <- tibble(
x = list("a", character())
)
df
#> # A tibble: 2 × 1
#> x
#> <list>
#> 1 <chr [1]>
#> 2 <chr [0]>
unnest_longer(df, x)
#> # A tibble: 1 × 1
#> x
#> <chr>
#> 1 a
unnest(df, x, keep_empty = TRUE)
#> # A tibble: 2 × 1
#> x
#> <chr>
#> 1 a
#> 2 <NA>
Created on 2022-03-28 by the reprex package (v2.0.1)
It may be as simple as passing keep_empty
through to the unchop()
call in unnest_longer()
, but I'd need to think about it critically to make sure
eutwt
Metadata
Metadata
Assignees
Labels
featurea feature request or enhancementa feature request or enhancementrectangling 🗄️converting deeply nested lists into tidy data framesconverting deeply nested lists into tidy data frames