I have the following df
city <- data.frame(City = c("London", "Liverpool", "Manchester","London", "Liverpool", "Manchester"),
Date = c("2016-08-05","2016-08-09","2016-08-10", "2016-09-05","2016-09-09","2016-09-10"))
I want to loop through it and get weather data by city$City
for the data in city$Date
city <- data.frame(City = c("London", "Liverpool", "Manchester","London", "Liverpool", "Manchester"),
Date = c("2016-08-05","2016-08-09","2016-08-10", "2016-09-05","2016-09-09","2016-09-10"),
Mean_TemperatureC = c("15","14","13","14","11","14"))
Currently I am using weatherData
to get weather data with the following funtion:
library(weatherData)
df <- getWeatherForDate("BOS", "2016-08-01")
Can someone help?
Here is a possibility:
The first step is to get the codes of the different cities with the
sub
and thegetStationCode
functions. We then get the vector with the mean of the temperatures, and finally, we create the data.framecity2
, with the correct column names.It is necessary to look for the stations code, as some cities (like Liverpool) could be on different countries (Canada and UK in this case). I checked the results on Weather Underground website for Liverpool, the results are correct.