tmfmnk
tmfmnk

Reputation: 40091

Looping through API and putting the results into single a data.frame

I have a list of unique object ID's which I would like to loop through API. The goal is to get a single data.frame with all objects and the obtained results.

This way I obtained the list of unique objects

    url  <- "http://mapakriminality.cz/api/"
    path <- "api/areas/?level=3"

    raw.result_codes <- GET(url = url, path = path)

    this.raw.content_codes <- rawToChar(raw.result_codes$content)

    this.content_codes <- fromJSON(this.raw.content_codes)

    this.content_codes_df <- as.data.frame(this.content_codes)

    this.content_codes_df <- this.content_codes_df[- c(523,
    524,
    525),]

    this.content_codes_list <- list(this.content_codes_df[,1])

This is how I was able to get the results for single object ID's. Areacode = an object from the list of codes.

url2  <- "http://mapakriminality.cz/api/"
path2 <- "api/crimes?areacode=190918&crimetypes=101-903&timefrom=1-2014$&timeto=12-2014&groupby=area"

raw.result_crimes <- GET(url = url2, path = path2)

this.raw.content_crimes <- rawToChar(raw.result_crimes$content)

this.content_crimes <- fromJSON(this.raw.content_crimes)

this.content_crimes_df <- as.data.frame(this.content_crimes)

The results are then looking like this:

  crimes.CrimeRate crimes.Found crimes.Solved
1         235.1567          604           449

What I am looking for are results like this:

  crimes.CrimeRate crimes.Found crimes.Solved    object_ID
1         235.1567          604           449    19091
2         X2                Y2            Z2     W2
3         X3                Y3            Z3     W3

Upvotes: 0

Views: 509

Answers (1)

DJack
DJack

Reputation: 4940

If I have correctly understood what you wanted to do, here is a simple loop (based on your code) to iterate through the different area codes, download the data and update a data frame (it is not the most efficient way to do it, but the solution is simple and easy to understand):

this.content_codes_list <- this.content_codes_df[,1]
url2  <- "http://mapakriminality.cz/api/"

this.content_crimes_df <- data.frame(crimes.Crime=rep(0,length(this.content_codes_list)),
                                     Rate.crimes.Found=rep(0,length(this.content_codes_list)),
                                     crimes.Solved=rep(0,length(this.content_codes_list)),
                                     object_ID=this.content_codes_list)

for (i in 1:length(this.content_codes_list)){
  print(i) #optional 
  path2 <- paste0("api/crimes?areacode=", 
                  this.content_codes_list[i],
                  "&crimetypes=101-903&timefrom=1-2014$&timeto=12-2014&groupby=area")
  raw.result_crimes <- GET(url = url2, path = path2)
  this.raw.content_crimes <- rawToChar(raw.result_crimes$content)
  this.content_crimes <- fromJSON(this.raw.content_crimes)
  this.content_crimes_df[i,1:3] <- as.data.frame(this.content_crimes)
}

EDIT

Sometimes the download data frame has more than one row or zero rows and the code above return an error. Therefore, I have updated the loop to make it works for all cases:

for (i in 1:length(this.content_codes_list)){
  print(i) #optional 
  path2 <- paste0("api/crimes?areacode=", 
                  this.content_codes_list[i],
                  "&crimetypes=101-903&timefrom=1-2014$&timeto=12-2014&groupby=area")
  raw.result_crimes <- GET(url = url2, path = path2)
  this.raw.content_crimes <- rawToChar(raw.result_crimes$content)
  this.content_crimes <- fromJSON(this.raw.content_crimes)
  output.df <- as.data.frame(this.content_crimes)
  if (nrow(output.df)!=0){
    output.df$object_ID <- this.content_codes_list[i]
    if (i==1){
      this.content_crimes_df <- output.df
    }else{
      this.content_crimes_df <- rbind.data.frame(this.content_crimes_df, output.df)
    }
  }
}

Result:

this.content_crimes_df

    crimes.CrimeRate crimes.Found crimes.Solved object_ID
1          8239.2655         3051           587    001110
2          2012.8364         2603           626    001111
3          2322.2749         1176           202    001112
4          3462.3317         2854           882    001114
5           268.7800          970           323    001115
6           422.5964         1556           414    001116
7           269.8192          679           241    001117
...

Upvotes: 1

Related Questions