Search code examples
rdarksky

Using darksky r package to scrape daily historical weather data


This is more of a conceptual question, sorry I am not posting a lot of original data. Any tips for using the darksky package to get daily historical weather data for a specific location? The api returns a complicated list, and I would like to store all of the lists in a single object. Here is what I have so far, but I don't think it is in the right direction. The ultimate goal is to pull hourly weather data for all of 2015 in to another dataframe that I have.



unique_dates <- seq(as.Date("2015/01/01"), as.Date("2015/12/30"), "days")


all_weather <- data.frame(
  loc = "Central Park",
  date = unique_dates, 
  lat =  as.numeric("40.766048"), 
  long = as.numeric("73.977320"), 
  stringsAsFactors = FALSE
)



Solution

  • Is this what you're looking for?

    Note that I changed the date range to a shorter period since free API calls are limited. I also changed the longitude to -73.977320, as I do not believe there is a Central Park in the Tian Shan mountains in Kyrgyzstan!

    library(darksky)
    library(lubridate)
    
    unique_dates <- seq(as.Date("2015/12/15"), as.Date("2015/12/30"), "days")
    
    weather_df <- unique_dates %>% 
      map(~get_forecast_for(40.766048, -73.977320, .x)) %>% 
      map_df("hourly") %>% 
      mutate(loc = "Central Park",
             date = date(time), 
             lat =  as.numeric("40.766048"), 
             long = as.numeric("-73.977320"))
    
    > head(weather_df)
                     time       summary                icon precipIntensity precipProbability temperature apparentTemperature dewPoint humidity pressure windSpeed
    1 2015-12-14 00:00:00 Mostly Cloudy partly-cloudy-night               0                 0       53.20               53.20    47.64     0.81   1019.1      3.73
    2 2015-12-14 01:00:00      Overcast              cloudy               0                 0       53.14               53.14    47.51     0.81   1018.7      5.21
    3 2015-12-14 02:00:00      Overcast              cloudy               0                 0       53.19               53.19    47.47     0.81   1018.1      4.24
    4 2015-12-14 03:00:00      Overcast              cloudy               0                 0       52.65               52.65    47.42     0.82   1017.6      6.36
    5 2015-12-14 04:00:00      Overcast              cloudy               0                 0       52.40               52.40    48.07     0.85   1016.0      5.09
    6 2015-12-14 05:00:00      Overcast              cloudy               0                 0       52.30               52.30    48.64     0.87   1015.8      3.98
      windGust windBearing cloudCover uvIndex visibility precipType precipAccumulation          loc       date      lat      long
    1     4.86          90       0.77       0      9.997       <NA>                 NA Central Park 2015-12-14 40.76605 -73.97732
    2     5.21          99       1.00       0      9.997       <NA>                 NA Central Park 2015-12-14 40.76605 -73.97732
    3     4.24          80       1.00       0      9.997       <NA>                 NA Central Park 2015-12-14 40.76605 -73.97732
    4     6.36          71       1.00       0      9.966       <NA>                 NA Central Park 2015-12-14 40.76605 -73.97732
    5     5.09          78       1.00       0      6.404       <NA>                 NA Central Park 2015-12-14 40.76605 -73.97732
    6     3.98          45       1.00       0      5.742       <NA>                 NA Central Park 2015-12-14 40.76605 -73.97732
    

    Plot

    weather_df %>% 
      ggplot(aes(x=time, y=temperature)) +
      geom_line()
    

    enter image description here