0

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.

1
  • 2
    do.call(cbind, df) or even do.call(data.frame, df) Commented Sep 10, 2020 at 20:44

1 Answer 1

2

This should work:

do.call(cbind, df)

Sign up to request clarification or add additional context in comments.

3 Comments

This is a nice solution
It is and works - thanks. Is there anything that works with pipes though?
You can do df %>% do.call(what = cbind) if you really want to use a pipe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.