0

My list is a series of characters each with different values, how should I attach this to my file for loop when I'm iterating over the files. Also if I were to specify that my "rfg_url_list$url_list[1]" included everything in the file so 'rfg_url_list$url_list' would it iterate one at a time throughout the entirety of the loop?

querydata <- 1
convertdata <- 2
binddata <- 3 
writedata <- 4

# Function for extracting data, names are indicative of whats going on in the loop.

 for (i in rfg_url_list$url_list[1]) {
  for (tempii in querydata) {
    for (tempiii in convertdata) {
      for (tempiv in binddata){

    querydata <- GET(i) # This sends the string to the API server
    convertdata <- content(querydata, type = 'application/json') # This converts API data to JSON
    binddata <- do.call(rbind.data.frame, convertdata[["_return"]][["traces"]][[1]][["trace"]]) # Binds JSON data to data frame
    writedata <- write.csv(binddata, file = "rainfalldata.csv", row.names = FALSE) # writes the data to a CSV file but needs to be done with different name based off list variables.

    
      } #4
    } #3 
  } #2
} #1
  • 1
    I don't understand what you are doing with those `temp` variables or why you have them. It also unclear exactly what you want the different iteration names to be. You can use `paste0()` to create whatever filename you want. It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Dec 02 '20 at 06:49
  • 1) This is just one loop, not 4. `temp*` are not used in the other loops. 2) Do not rewrite the loop control in the loops, `querydata`, etc are only evaluated once. 3) Can you post the output of `dput(head(convertdata[["_return"]][["traces"]][[1]][["trace"]]))`? – Rui Barradas Dec 02 '20 at 06:50
  • @MrFlick I'm just looking back at the code and have realized that it was unnecessary to have these extra loops as my understanding of how they worked was flawed. – hydrology_guy22 Dec 03 '20 at 00:18
  • @RuiBarradas Hey I've fixed 1.), however I'm unsure what you mean with re-writing the loop controls. Do you mean like specifying the output of the command e.g querydata <- get(i)? The function is meant to iterate over a list of 808 variables however in rfg_url_list$url_list[1] I've only specified because I wanted to test and isolate one single value. The output of the convertdata is a dataframe with 3 variables for a historical rainfall gauge. Apologies if I can't be more clear - I'm relatively new to coding and R in general. – hydrology_guy22 Dec 03 '20 at 00:27

0 Answers0