I have tibble with with i.e. two columns and only one row. Each cell contains a list. I would like to unnest them 'longer', so that each element of list has its own row. The problem seems to be that the lists have different lengths, what implies that one column (with the shorter list) should have NAs at the end.
I saw this related post, but was wondering whether there is a more elegant way.
Many thanks. r
library(tidyverse)
#what I have
my_df <- tibble(country1=list(c("country1_case1", "country2_case2")),
country2=list(c("country2_case1", "country2_case2", "country2_cases3")))
#what I want:
goal_df <- tibble::tribble(
~country1, ~country2,
"country1_case1", "country2_case1",
"country1_case2", "country2_case2",
"NA", "country2_case3")
#something like?
df %>%
unnest_longer(col=c(country1, country2))
#> Error: object 'country1' not found
Siddharth's solution is perfectly great, but just for fun I wanted to have a go with a slightly different approach. I must say I needed the length<-() function though, the first time I've used one of those <- functions, that I'm aware of. So that was a nice piece of learning for me.