How can I populate my local data frame with weather data from weather underground?
For instance I have this local data frame:
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")
So currently it looks like this:
And I am going to pull weather date in London area:
http://api.wunderground.com/api/APIKEY/geolookup/conditions/q/UK/London.json
I will get a set of data like this:
{
"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":""
}
}
So I want to merge this weather data with my data frame so I can make it into this - when both timestamps (local and weather underground) are matched or closed:
particles timestamp date ws wd humidity temperature
xx xxx xx 4 300 72 14
and so on...
Is it possible?
Or is there any other alternatives for weather underground?
Load libraries in your workspace
library(RJSONIO)
library(purrr)
Get the names of the list in the json file - temp.json
names_json <- names(fromJSON("temp.json"))
Get the list of observations
obs_list <- fromJSON("temp.json")[["current_observation"]]
Note down the parameters in a list
params <- list("observation_epoch", "local_tz_long", "wind_string", "wind_degrees", "relative_humidity", "temp_f", "temp_c")
Using map
function from purrr
package, loop through the params
list and get the values from the obs_list
new_df <- data.frame(map(.x = params, .f = ~ {obs_list[[.x]]}))
set the names of new_df
as defined in params
names(new_df) <- params
Convert the character date into integer date
new_df$observation_epoch <- as.integer(new_df$observation_epoch)
Load data.table library into workspace
library(data.table)
Convert new_df and local_df dataframes into datatables by reference
setDT(new_df)
setDT(local_df)
Convert Integer date into posix date for observation_epoch
in new_df
new_df[, observation_epoch := as.POSIXct(observation_epoch, origin = "1970-01-01", tz = local_tz_long)]
Combine two datatables based on dates and if there is no match, add NA to it.
local_df[new_df, on = .(date == observation_epoch), nomatch = NA]
Output:
# 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
Your local_df as per your question do not have a matching date. So I added a matching date for local_df
from new_df
as follows
local_df$date[1]
# [1] "2016-07-31 07:18:53 BST"
local_df$date[1] <- new_df$observation_epoch[1]
Now merge the two datatables again
local_df[new_df, on = .(date == observation_epoch), nomatch = NA]
Output
# 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