I have a dataframe created from JSON returned through an API.
df <- structure(list(date = c("2020-09-10", "2020-09-09", "2020-09-08",
"2020-09-07", "2020-09-06"), name = c("England", "England", "England",
"England", "England"), code = c("E92000001", "E92000001", "E92000001",
"E92000001", "E92000001"), cases = structure(list(daily = c(2578L,
2286L, 2094L, 2528L, 2576L), cumulative = c(309133L, 306555L,
304269L, 302175L, 299647L)), class = "data.frame", row.names = c(NA,
-5L)), deaths = structure(list(daily = c(0L, 6L, 8L, 11L, 9L),
cumulative = c(36944L, 36944L, 36938L, 36930L, 36919L)), class = "data.frame", row.names = c(NA,
-5L))), class = "data.frame", row.names = c(NA, -5L))
If you take a look at the data with: head(df)
you get this:
date name code cases.daily cases.cumulative deaths.daily deaths.cumulative
1 2020-09-10 England E92000001 2578 309133 0 36944
2 2020-09-09 England E92000001 2286 306555 6 36944
3 2020-09-08 England E92000001 2094 304269 8 36938
4 2020-09-07 England E92000001 2528 302175 11 36930
5 2020-09-06 England E92000001 2576 299647 9 36919
but
colnames(df)
gives:
[1] "date" "name" "code" "cases" "deaths"
I know there are 2 nested DFs in there, but I can't figure out how to use something like:
unnest(cols = c(cases, deaths))
... to get 7 columns in an un-nested DF with the column names and structure as in the head example above. I'd prefer to be able to do this in a tidyverse pipe to avoid creating intermediate objects.
do.call(cbind, df)or evendo.call(data.frame, df)