I'm looking for simple solution to the above. I seem to run into this problem frequently when APIs return JSON which is subsequently converted to a list.
Reprex data:
result_head <- list(list(name = "JEFFREY", gender = "male", probability = 1L,
count = 932L), list(name = "Jan", gender = "male", probability = 0.6,
count = 1663L), list(name = "Elquis", gender = NULL), list(
name = "ELQUIS", gender = NULL), list(name = "Francisco",
gender = "male", probability = 1L, count = 1513L))
The task is, as simply as possible to convert this to a 5 row data frame. Given the items within each list element are irregular, NA
s will need to introduced for missing items similar to how bind_rows
works when stacking data frames with irregular columns.
What I've tried:
map_dfr(result, bind_rows)
do.call(bind_rows, result_head)
flatten(result_head)
bind_rows(flatten(result_head))
I asked a similar question here: Extracting to a data frame from a JSON generated multi-level list with occasional missing elements
... but the solution is totally over-engineered for a less complex list.
I'd like a solution that is hopefully elegant as possible - I run into this kind of operation so often and yet there doesn't seem to be a consistent way of doing this with few levels of function abstraction.
I realise questions around this may have already been asked and I may have missed something, but there doesn't seem to be a consistent and simple way of tackling what seems a common problem.
Thanks.