Hello,
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
Created on 2020-09-10 by the reprex package (v0.3.0)