rloopslapplyweatherdata

How to loop through a list of cities and get temparature for given date with 'weatherData' in R


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?


Solution

  • Here is a possibility:

    temp <- as.matrix(city)
    codes <- sapply(1:nrow(city), function(x) getStationCode(city[x,1], "GB")[[1]])
    station <- sub("^[[:print:]]+\\s([A-Z]{4})\\s[[:print:]]+", "\\1", codes)
    
    temp[, 1] <- station
    
    temperature <- sapply(1:nrow(temp), function(x) {getWeatherForDate(temp[x, 1], temp[x, 2])$Mean_TemperatureC})
    city2 <- setNames(cbind(city, temperature), c(colnames(city), "Mean_TemperatureC"))
    
    city2
    #        City       Date Mean_TemperatureC
    # 1     London 2016-08-05                14
    # 2  Liverpool 2016-08-09                14
    # 3 Manchester 2016-08-10                13
    # 4     London 2016-09-05                20
    # 5  Liverpool 2016-09-09                18
    # 6 Manchester 2016-09-10                13
    

    The first step is to get the codes of the different cities with the sub and the getStationCode functions. We then get the vector with the mean of the temperatures, and finally, we create the data.frame city2, 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.