0

I am a huge R fan, but it never seems to work out for me, I am trying to use an API to get weather data, but I cannot write the loop. I have all the codes in the right format, but when I import the file into r, the cells appear like

-33.86659241, 151.2081909, \"2014-10-01T02:00:00"\

and this is preventing me from running the code. So rather than using a loop I need to use a mailmerge to create 5000 lines of code. Any help would be really appreciated.

tmp <- get_forecast_for(-33.86659241, 151.2081909, "2014-10-01T02:00:00", add_headers=TRUE)
    fdf <- as.data.frame(tmp)
    fdf$ID <- "R_3nNli1Hj2mlvFVo"
    fd <- rbind(fd,fdf) 

Here is the code with loop -

df <- read.csv("~/Machine Learning/Darksky.csv", header=T,sep=",", fill = TRUE)

for(i in 1:length(df$DarkSky)){
  fdf <- get_forecast_for(df$LocationLatitude[i], df$LocationLongitude[i], df$DarkSky[i], add_headers=TRUE)
  fdf <- as.data.frame(fdf)
  fdf <- fdf[1:2,]
  fd <- rbind(fd,fdf)
}

I also wanted to rbind the retreived data onto a dataframe but it does not work. I also wanted to cbind the identifier, which would be the value in df$DarkSky[i], but it will not work.

CSV -

LocationLatitude    LocationLongitude   DarkSky
-33.86659241        151.2081909         "2014-10-01T02:00:00"

The get_forecast_for function takes three parameters, the latitude, longitude and the date, structured as above, I have the loop working for latitude and longitude, but the time/date is not working.

Lowpar
  • 897
  • 10
  • 31
  • 1
    Can you provide an example of what the data look like in the CSV? Paste the first few lines into your question, as they appear in the raw file. – jdobres Oct 11 '16 at 20:04
  • Thanks, I have edited my code and displayed the csv code – Lowpar Oct 11 '16 at 20:13
  • You say that it "does not work" or "will not work" but what exactly does that mean? Are you getting an error message? What exactly is happening and what did you expect to happen? It doesn't seem like all your variables are being defined. Where does `fd` come from. Make sure you include a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to make it possible to diagnose the problem and help you. – MrFlick Oct 11 '16 at 20:25
  • Latitude and Longitude work, but when I pass the code the DarkSky[i] it errors and says Error in get_forecast_for(df$LocationLatitude[i], df$LocationLongitude[i], : Not Found (HTTP 404). I wonder are the inverted commas causing problems – Lowpar Oct 11 '16 at 20:42
  • I took out the inverted commas and it worked, in term of the data frame, I want to rbind each retrieved instance of the data onto the dataframe. – Lowpar Oct 11 '16 at 20:55
  • @Lowpar;hI;I am also using Darksky to get the weather data, but i got a problem during calling data, when i ran the code: now <- get_current_forecast(43.2672, -70.8617) then i typed the API key, after that i got the return: Updating DARKSKY_API_KEY env var to PAT Error in curl::curl_fetch_memory(url, handle = handle) : Timeout was reached do you know what happened there, or could you please describe how it works in R studio? thanks a lot – ZAWD Jan 23 '17 at 14:50
  • @ZAWD did you include the time?? get_current_forecast(43.2672, -70.8617, "2014-10-01T02:00:00", add_headers=TRUE) – Lowpar Jan 23 '17 at 15:38
  • Hi , i think i have included the time as well :) – ZAWD Jan 23 '17 at 16:28
  • Does the API support the country you are looking for? I would check a normal post and see does it work, by posting manually on the website. This worked for me... – Lowpar Jan 23 '17 at 17:04

0 Answers0