【发布时间】:2016-07-31 06:35:51
【问题描述】:
如何使用来自weather underground 的天气数据填充我的本地数据框?
例如我有这个本地数据框:
structure(list(particles = c(1, 2, 3, 4, 5, 6), timestamp = c(1469945933290,
1469945937786, 1469945940819, 1469945944000, 1469945948113, 1469945951115
), date = structure(c(1469945933.29, 1469945937.786, 1469945940.819,
1469945944, 1469945948.113, 1469945951.115), class = c("POSIXct",
"POSIXt"), tzone = "UTC-1")), .Names = c("particles", "timestamp",
"date"), row.names = c(NA, -6L), class = "data.frame")
所以目前看起来是这样的:
我要拉伦敦地区的天气日期:
http://api.wunderground.com/api/APIKEY/geolookup/conditions/q/UK/London.json
我会得到一组这样的数据:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"geolookup": 1
,
"conditions": 1
}
}
, "location": {
"type":"INTLCITY",
"country":"UK",
"country_iso3166":"GB",
"country_name":"United Kingdom",
"state":"",
"city":"London",
"tz_short":"BST",
"tz_long":"Europe/London",
"lat":"51.47999954",
"lon":"-0.44999999",
"zip":"00000",
"magic":"1",
"wmo":"03772",
"l":"/q/zmw:00000.1.03772",
"requesturl":"global/stations/03772.html",
"wuiurl":"https://www.wunderground.com/global/stations/03772.html",
"nearby_weather_stations": {
"airport": {
"station": [
{ "city":"London / Heathrow Airport", "state":"", "country":"United Kingdom", "icao":"", "lat":"51.47920609", "lon":"-0.45060000" }
,{ "city":"London", "state":"", "country":"UK", "icao":"EGLL", "lat":"51.47750092", "lon":"-0.46138901" }
,{ "city":"Northolt", "state":"", "country":"UK", "icao":"EGWU", "lat":"51.54868317", "lon":"-0.41691700" }
,{ "city":"Farnborough", "state":"", "country":"UK", "icao":"EGLF", "lat":"51.27999115", "lon":"-0.77269602" }
]
}
,
"pws": {
"station": [
{
"neighborhood":"Burns Way",
"city":"Hounslow",
"state":"",
"country":"GB",
"id":"IHOUNSLO7",
"lat":51.476894,
"lon":-0.394850,
"distance_km":3,
"distance_mi":2
},
{
"neighborhood":"Willowbrook Road",
"city":"Stanwell",
"state":"",
"country":"UK",
"id":"ISTANWEL3",
"lat":51.445091,
"lon":-0.469712,
"distance_km":4,
"distance_mi":2
},
{
"neighborhood":"Stanwell",
"city":"Staines-upon-Thames",
"state":"",
"country":"GB",
"id":"ISTAINES5",
"lat":51.444927,
"lon":-0.469693,
"distance_km":4,
"distance_mi":2
},
{
"neighborhood":"",
"city":"Iver, South Bucks",
"state":"",
"country":"GB",
"id":"IBUCKSIV1",
"lat":51.504894,
"lon":-0.508861,
"distance_km":4,
"distance_mi":2
},
{
"neighborhood":"Thetford Road",
"city":"Stanwell",
"state":"",
"country":"GB",
"id":"ISTANWEL7",
"lat":51.437420,
"lon":-0.473234,
"distance_km":5,
"distance_mi":3
},
{
"neighborhood":"Ashford Surrey",
"city":"Stanwell",
"state":"",
"country":"GB",
"id":"ISTANWEL2",
"lat":51.437420,
"lon":-0.473234,
"distance_km":5,
"distance_mi":3
},
{
"neighborhood":"Oakfield Road",
"city":"Shepperton",
"state":"",
"country":"GB",
"id":"ISHEPPER3",
"lat":51.431770,
"lon":-0.452568,
"distance_km":5,
"distance_mi":3
},
{
"neighborhood":"Hewens Road",
"city":"Uxbridge",
"state":"",
"country":"GB",
"id":"IUXBRIDG6",
"lat":51.528568,
"lon":-0.440460,
"distance_km":5,
"distance_mi":3
},
{
"neighborhood":"Nelson Road",
"city":"Stanwell",
"state":"",
"country":"GB",
"id":"ISTANWEL5",
"lat":51.430702,
"lon":-0.470822,
"distance_km":5,
"distance_mi":3
},
{
"neighborhood":"Waters Drive",
"city":"Staines-upon-Thames",
"state":"",
"country":"UK",
"id":"ISTAINES3",
"lat":51.437225,
"lon":-0.508767,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"Iver",
"city":"Iver",
"state":"",
"country":"GB",
"id":"IIVER4",
"lat":51.520706,
"lon":-0.512615,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"East Berkshire Weather",
"city":"Slough",
"state":"",
"country":"GB",
"id":"ISLOUGH24",
"lat":51.506660,
"lon":-0.529796,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"Wills Crescent",
"city":"Isleworth",
"state":"",
"country":"UK",
"id":"IISLEWOR11",
"lat":51.457008,
"lon":-0.367131,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"Hounslow",
"city":"Hounslow",
"state":"Middlesex",
"country":"GB",
"id":"IMIDDLES2",
"lat":51.456646,
"lon":-0.366677,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"Adelphi Crescent",
"city":"Hayes",
"state":"",
"country":"GB",
"id":"IHAYES3",
"lat":51.534149,
"lon":-0.421576,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"Attlee Road",
"city":"Hayes",
"state":"",
"country":"UK",
"id":"IHAYES2",
"lat":51.533058,
"lon":-0.407573,
"distance_km":6,
"distance_mi":3
},
{
"neighborhood":"Langley Weather Station",
"city":"Langley , Slough",
"state":"BERKSHIRE. U.K.",
"country":"GB",
"id":"IBERKSHI18",
"lat":51.498505,
"lon":-0.541826,
"distance_km":6,
"distance_mi":4
},
{
"neighborhood":"Constance Road",
"city":"Isleworth",
"state":"",
"country":"GB",
"id":"IISLEWOR4",
"lat":51.450520,
"lon":-0.362289,
"distance_km":6,
"distance_mi":4
},
{
"neighborhood":"",
"city":"Iver",
"state":"BUCKINGHAMSHIRE",
"country":"GB",
"id":"IBUCKING43",
"lat":51.514481,
"lon":-0.535723,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Prospect Crescent",
"city":"Isleworth",
"state":"",
"country":"UK",
"id":"IISLEWOR7",
"lat":51.453918,
"lon":-0.356965,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Sunbury on Thames",
"city":"Middlesex",
"state":"UK",
"country":"GB",
"id":"I90581027",
"lat":51.417065,
"lon":-0.433550,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Staines",
"city":"Staines-upon-Thames",
"state":"",
"country":"GB",
"id":"ISTAINES2",
"lat":51.422482,
"lon":-0.494401,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Hythe Road",
"city":"Staines-upon-Thames",
"state":"",
"country":"GB",
"id":"ISTAINES4",
"lat":51.431992,
"lon":-0.521418,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Hampton",
"city":"Hampton",
"state":"",
"country":"UNITED KINGDOM",
"id":"IHAMPTON7",
"lat":51.430706,
"lon":-0.379594,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Clifton Road",
"city":"Hounslow",
"state":"",
"country":"GB",
"id":"IHOUNSLO4",
"lat":51.474903,
"lon":-0.344303,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Osterley Crescent",
"city":"Hounslow",
"state":"",
"country":"UK",
"id":"IHOUNSLO6",
"lat":51.479061,
"lon":-0.337751,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Morris Avenue",
"city":"Uxbridge",
"state":"",
"country":"UK",
"id":"IUXBRIDG7",
"lat":51.549801,
"lon":-0.465780,
"distance_km":7,
"distance_mi":4
},
{
"neighborhood":"Fortescue Avenue",
"city":"Twickenham",
"state":"",
"country":"UK",
"id":"ITWICKEN15",
"lat":51.439217,
"lon":-0.356578,
"distance_km":7,
"distance_mi":4
}
]
}
}
}
, "current_observation": {
"image": {
"url":"http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title":"Weather Underground",
"link":"http://www.wunderground.com"
},
"display_location": {
"full":"London, United Kingdom",
"city":"London",
"state":"",
"state_name":"United Kingdom",
"country":"UK",
"country_iso3166":"GB",
"zip":"00000",
"magic":"1",
"wmo":"03772",
"latitude":"51.47999954",
"longitude":"-0.44999999",
"elevation":"24.00000000"
},
"observation_location": {
"full":"London, ",
"city":"London",
"state":"",
"country":"UK",
"country_iso3166":"GB",
"latitude":"51.47750092",
"longitude":"-0.46138901",
"elevation":"79 ft"
},
"estimated": {
},
"station_id":"EGLL",
"observation_time":"Last Updated on July 31, 7:20 AM BST",
"observation_time_rfc822":"Sun, 31 Jul 2016 07:20:00 +0100",
"observation_epoch":"1469946000",
"local_time_rfc822":"Sun, 31 Jul 2016 07:26:14 +0100",
"local_epoch":"1469946374",
"local_tz_short":"BST",
"local_tz_long":"Europe/London",
"local_tz_offset":"+0100",
"weather":"Partly Cloudy",
"temperature_string":"59 F (15 C)",
"temp_f":59,
"temp_c":15,
"relative_humidity":"72%",
"wind_string":"From the Variable at 4 MPH",
"wind_dir":"Variable",
"wind_degrees":0,
"wind_mph":4,
"wind_gust_mph":0,
"wind_kph":6,
"wind_gust_kph":0,
"pressure_mb":"1017",
"pressure_in":"30.04",
"pressure_trend":"+",
"dewpoint_string":"50 F (10 C)",
"dewpoint_f":50,
"dewpoint_c":10,
"heat_index_string":"NA",
"heat_index_f":"NA",
"heat_index_c":"NA",
"windchill_string":"NA",
"windchill_f":"NA",
"windchill_c":"NA",
"feelslike_string":"59 F (15 C)",
"feelslike_f":"59",
"feelslike_c":"15",
"visibility_mi":"6.2",
"visibility_km":"10.0",
"solarradiation":"--",
"UV":"1","precip_1hr_string":"-9999.00 in (-9999.00 mm)",
"precip_1hr_in":"-9999.00",
"precip_1hr_metric":"--",
"precip_today_string":"0.00 in (0.0 mm)",
"precip_today_in":"0.00",
"precip_today_metric":"0.0",
"icon":"partlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif",
"forecast_url":"http://www.wunderground.com/global/stations/03772.html",
"history_url":"http://www.wunderground.com/history/airport/EGLL/2016/7/31/DailyHistory.html",
"ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=51.47750092,-0.46138901",
"nowcast":""
}
}
所以我想将这个天气数据与我的数据框合并,这样我就可以把它变成这个 - 当两个时间戳(本地和地下天气)匹配或关闭 :
particles timestamp date ws wd humidity temperature
xx xxx xx 4 300 72 14
and so on...
有可能吗?
或者还有其他替代地下天气的方法吗?
【问题讨论】:
-
看看
weatherData包。如果这提供了您需要的数据,它可能比编写自己的解决方案更容易。 -
就是这样。下载愉快。
-
@Benjamin 谢谢!
-
forecast.io 是另一个网站,它允许每天在免费层级进行两倍的 API 调用,并且还具有 JSON 接口。这里有一个实现它的包。 github.com/hrbrmstr/Rforecastio
标签: r weather-api