medavis6 medavis6 - 1 month ago 25
R Question

List of Data Frames to One Data Frame

Disclaimer: I know that this question has been asked before. The answer provided in this answer worked for me in the past, but for some reason has stopped now.

I am pulling Marketing email statistics from the Mailchimp API. I have been doing this for the last half year or so. However, in the past 2 months, I believe the structure of what I pull has changed and thus, my code no longer works and I cannot figure out why. I believe it has something to do with the nested data frames within my list of data frames that I receive.

Here is an example of my code and the resulting list of data frames. I have removed sensitive information from my code and image:

library(httr)
library(jsonlite)
library(plyr)

#Opens-----------
opens1 <- GET("https://us4.api.mailchimp.com/3.0/reports/***ReportNumber***/sent-to?count=4000",authenticate('***My Company***', '***My-Password***'))
opens1 <- content(opens1,"text")
opens1 <- fromJSON(opens1)


enter image description here

Then I run
opens1 <- ldply(opens1, data.frame)
, and I receive the following error:

Error in allocate_column(df[[var]], nrows, dfs, var) :
Data frame column 'merge_fields' not supported by rbind.fill


I tried using and looking up
rbind.fill()
and the other methods described in the linked answer at the top of my post, to no avail. What am I interpreting incorrectly about the
merge_fields
variable, or am I way off, and how do I correct it?

I'm just trying to get one data frame of all of the variables from the
opens1
list.

Thanks for any and all help, and please, feel free to ask any clarification questions!

Answer

On a quick glance, this seems to work for me:

library(httr)
campaign_id <- "-------"
apikey = "------"
url <- sprintf("https://us1.api.mailchimp.com/3.0/reports/%s/sent-to", campaign_id)
opens <- GET(url, query = list(apikey = apikey, count = 4000L))
lst <- rjson::fromJSON(content(opens, "text"))
df <- dplyr::bind_rows(
  lapply(lst$sent_to, function(x) 
    as.data.frame(t(unlist(x)), stringsAsFactors = F)
))