For loop in R with getWeatherForDate

140 views Asked by At

new in R, so please forgive for the lack of knowledge. I need to take the weather data for a specific time interval in R using wunderground. There is a command for that, but it is not hourly. I need to obtain the hourly data from 2015 to 2017. So, I need to write a for loop. The code which is used for a single date for the specific weather conditions is:

weather <- getWeatherForDate("IMUU011F4", "2015-01-01", 
  station_type = "id", opt_detailed = TRUE, opt_custom_columns = TRUE, 
  custom_columns =  c(3, 7, 9)) 

So, I need to use this code for all days in 2 year, have a single file for 2 years.

I can't able to write the for loop.

Can anyone help me.

Thank you.

1

There are 1 answers

10
Len Greski On

No loop is required to pull a single weather station for 2 years. Instead, use the end_date= argument. Since you're starting on January 1, 2015, the end date would be December 31, 2016.

library(weatherData)
weather<-getWeatherForDate("IMUU011F4", "2015-01-01", "2016-12-31",
                           station_type="id", 
                           opt_detailed=T, opt_custom_columns=T, 
                           custom_columns= c(3,7,9))

The download process writes a lot of data to the R console, starting with the following:

Checking Data Availability For IMUU011F4
Found 141 records for 2015-01-01
Found 145 records for 2016-12-31

Data is Available for the interval.

Will be fetching these Columns:
[1] "Time"         "DewpointF"    "WindSpeedMPH" "Humidity"    
Begin getting Daily Data for IMUU011F4
IMUU011F4 1 2015-01-01 : Fetching 282 Rows with 4 Column(s)
IMUU011F4 2 2015-01-02 : Fetching 305 Rows with 4 Column(s)
IMUU011F4 3 2015-01-03 : Fetching 313 Rows with 4 Column(s)
IMUU011F4 4 2015-01-04 : Fetching 253 Rows with 4 Column(s)
IMUU011F4 5 2015-01-05 : Fetching 318 Rows with 4 Column(s)
IMUU011F4 6 2015-01-06 : Fetching 319 Rows with 4 Column(s)
IMUU011F4 7 2015-01-07 : Fetching 335 Rows with 4 Column(s)
IMUU011F4 8 2015-01-08 : Fetching 349 Rows with 4 Column(s)
IMUU011F4 9 2015-01-09 : Fetching 332 Rows with 4 Column(s)
IMUU011F4 10 2015-01-10 : Fetching 344 Rows with 4 Column(s)
  .
  .
  .

The resulting data frame looks like this.

> nrow(weather)
[1] 203015
> summary(weather)
      Time                       DewpointF      WindSpeedMPH       Humidity    
 Min.   :2015-01-01 00:04:00   Min.   : 5.40   Min.   : 0.000   Min.   : 0.00  
 1st Qu.:2015-06-27 18:01:30   1st Qu.:47.20   1st Qu.: 0.000   1st Qu.:58.00  
 Median :2015-12-18 11:18:00   Median :54.90   Median : 5.400   Median :69.00  
 Mean   :2015-12-26 23:42:55   Mean   :54.36   Mean   : 5.877   Mean   :68.73  
 3rd Qu.:2016-06-24 00:42:00   3rd Qu.:64.40   3rd Qu.: 9.200   3rd Qu.:80.00  
 Max.   :2016-12-31 23:58:00   Max.   :78.80   Max.   :43.400   Max.   :99.00  
> head(weather)
                 Time DewpointF WindSpeedMPH Humidity
1 2015-01-01 00:04:00      51.0         11.4       89
2 2015-01-01 00:11:00      51.0         13.0       89
3 2015-01-01 00:17:00      51.0         15.2       89
4 2015-01-01 00:22:00      51.0          8.3       89
5 2015-01-01 00:27:00      51.5         11.4       90
6 2015-01-01 00:32:00      51.5          9.2       90
> 

If you want to obtain the weather for multiple weather stations, this can be accomplished with an apply() function. Note that this will take a number of minutes to execute, since the IMUU011F4 weather station generated over 203,000 rows of output for the 2 year data request.

theStations <- c("IMUU011F4","KFLMIAMI75","IMOSCOWO2")
weatherList <- lapply(theStations, function (x) {
    getWeatherForDate(x, "2015-01-01", "2016-12-31",
                               station_type="id", 
                               opt_detailed=T, opt_custom_columns=T, 
                               custom_columns= c(3,7,9))

}) 
weather <- do.call(rbind,weatherList)