Sam Comber
Sam Comber

Reputation: 1293

Appending rows to dataframe inside for loop

Essentially, I'm pulling data for every month in 2016 from an API and converting the resulting JSON into a dataframe. So, I loop over an array of months which are appended to the URL string for the API call.

Here is my code:

dates = c("2015-12", "2016-01", "2016-02", "2016-03", "2016-04", "2016-05", "2016-06", "2016-07", "2016-08", "2016-09", "2016-10", "2016-11")

for (month in dates) {
    url = sprintf("https://data.police.uk/api/crimes-street/all-crime?poly=53.5803,-2.6882:53.5803,-2.6882:53.2307,-3.2389:53.2307,-3.2389&date=%s", month)
    r <- httr::GET(url)
    json <- httr::content(r, "text")

    document <- jsonlite::fromJSON(txt=json)
}

What I've tried to do is use rbind inside the loop to append upon every iteration, but the resulting dataframe is always empty.

My question is: inside this for loop, how would I iterate over each month and append each dataframe produced by the fromJSON function to each other into a master dataframe?

Thanks,

Sam

Upvotes: 0

Views: 509

Answers (1)

Roland
Roland

Reputation: 132999

Do not grow an object in a loop, in particular not a data.frame. That's incredibly slow. Put them into a list and rbind them in one step:

res <- lapply(dates, function(month) {
  url = sprintf("https://data.police.uk/api/crimes-street/all-crime?poly=53.5803,-2.6882:53.5803,-2.6882:53.2307,-3.2389:53.2307,-3.2389&date=%s", month)
  r <- GET(url)
  json <- content(r, "text")

  fromJSON(txt=json)
  })

do.call(rbind, res)

Upvotes: 2

Related Questions