Suppose I have this data frame df
:
> df <- data.frame(a=c(1,2))
> df$b <- list(list(),list(id=c('d', 'e')))
> df
a b
1 1 NULL
2 2 d, e
> df$b
[[1]]
list()
[[2]]
[[2]]$id
[1] "d" "e"
How do I get the following data frame:
> df2 <- data.frame(a=c(1,2,2),b=c(NA,'d','e'))
> df2
a b
1 1 <NA>
2 2 d
3 2 e
In other words, there should be a new row for every list item in column b
.
I looked at these three things:
jsonlite::flatten(df)
rlang::flatten(df)
purrr::flatten(df)
and also these two questions, and nothing seemed quite right.
Yes, I'm getting this data from an API that returns json.