R - 如何填充地下天气的天气数据?

时间:2016-07-31 06:35:52

标签: r weather-api

如何使用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")

所以目前它看起来像这样:

enter image description here

我打算在伦敦地区拉出天气日期:

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... 

有可能吗?

或者地下天气还有其他选择吗?

1 个答案:

答案 0 :(得分:2)

在工作区中加载库

library(RJSONIO)
library(purrr)

在json文件中获取列表的名称 - temp.json

names_json <- names(fromJSON("temp.json"))

获取观察列表

obs_list <- fromJSON("temp.json")[["current_observation"]]

记下列表中的参数

params <- list("observation_epoch", "local_tz_long", "wind_string", "wind_degrees", "relative_humidity", "temp_f", "temp_c")

使用map包中的purrr功能,循环浏览params列表并获取obs_list

中的值
new_df <- data.frame(map(.x = params, .f = ~ {obs_list[[.x]]}))

根据new_df

中的定义设置params的名称
names(new_df) <- params

将字符日期转换为整数日期

new_df$observation_epoch <- as.integer(new_df$observation_epoch)

将data.table库加载到工作区

library(data.table)

通过引用

将new_df和local_df数据帧转换为数据表
setDT(new_df)
setDT(local_df)

将整数日期转换为observation_epoch

new_df的posix日期
new_df[, observation_epoch := as.POSIXct(observation_epoch, origin = "1970-01-01", tz = local_tz_long)]

根据日期合并两个数据表,如果没有匹配,则为其添加NA。

local_df[new_df, on = .(date == observation_epoch), nomatch = NA]

<强>输出:

#  particles timestamp                date local_tz_long                wind_string wind_degrees relative_humidity temp_f   temp_c
# 1:     NA        NA 2016-07-31 07:20:00 Europe/London From the Variable at 4 MPH            0               72%     59  15 

根据您的问题,您的local_df没有匹配的日期。所以我在local_df添加了new_df的匹配日期,如下所示

local_df$date[1]
# [1] "2016-07-31 07:18:53 BST"

local_df$date[1] <- new_df$observation_epoch[1]

现在再次合并两个数据表

local_df[new_df, on = .(date == observation_epoch), nomatch = NA]

<强>输出

#   particles    timestamp                date local_tz_long                wind_string wind_degrees relative_humidity temp_f    temp_c
# 1:        1 1.469946e+12 2016-07-31 07:20:00 Europe/London From the Variable at 4 MPH            0               72%     59    15