首页 > 解决方案 > 使用 darksky r 包抓取每日历史天气数据

问题描述

这更像是一个概念性问题,抱歉我没有发布很多原始数据。使用 darksky 包获取特定位置的每日历史天气数据的任何提示?api 返回一个复杂的列表,我想将所有列表存储在一个对象中。这是我到目前为止所拥有的,但我认为它的方向不正确。最终目标是将 2015 年全年的每小时天气数据提取到我拥有的另一个数据框中。



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
)


标签: rdarksky

解决方案


这是你要找的吗?

请注意,我将日期范围更改为较短的时间段,因为免费 API 调用是有限的。我也把经度改成了-73.977320,因为我不相信吉尔吉斯斯坦的天山有中央公园!

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

阴谋

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

在此处输入图像描述


推荐阅读