0

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.

jay.sf
  • 60,139
  • 8
  • 53
  • 110
M.Yen
  • 1
  • 1

1 Answers1

0

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)
Len Greski
  • 10,505
  • 2
  • 22
  • 33
  • It says "Data is Not Available". But when I look at for a single day, there is a data. – M.Yen Nov 26 '17 at 16:47
  • @M.Yen - we need more context regarding your comment about "data not available." – Len Greski Nov 26 '17 at 17:08
  • @M.Yen - It appears that the 2016 data for weather station IMUU011F4 is partially missing for [August 24th](https://www.wunderground.com/personal-weather-station/dashboard?ID=IMUU011F4#history/s20160824/e20160824/mdaily) and [September 11th](https://www.wunderground.com/personal-weather-station/dashboard?ID=IMUU011F4#history/s20160911/e20160911/mdaily), and completely missing for August 25th through September 10th, based on what is written to the log. There are also a couple of days reported as having embedded NULL values: July 17th and August 17th. – Len Greski Nov 26 '17 at 17:33
  • It says "Warning messages: 1: In cleanAndSubsetDetailedData(wxdata, date, opt_temperature_columns, : Some columns are missing in Obtained Data. Check" Even though there are missing cells, I need to provide the data. Is there any way to obtain the data with NULL or missing cells? – M.Yen Nov 26 '17 at 20:14
  • @M.Yen - were you able to download the data with the answer I provided? If so, please accept the answer. Post another question regarding how to clean the data once you've downloaded it. – Len Greski Nov 26 '17 at 20:29
  • No, I couldn't get the data. It says NULL(empty) – M.Yen Nov 26 '17 at 21:41
  • @M.Yen - what function calls did you use, and what specific error messages did you receive at each step? – Len Greski Nov 27 '17 at 00:23
  • I used library(weatherData) weather_mugla<-getWeatherForDate("IMUU011F4", "2015-01-01", "2017-09-02", station_type="id", opt_detailed=T, opt_custom_columns=T, custom_columns= c(3,7,9)) . My time interval is changed. – M.Yen Nov 27 '17 at 02:26
  • I just only receive an error messages at the end. It says - Data is Not Available Warning messages: 1: In cleanAndSubsetDetailedData(wxdata, date, opt_temperature_columns, : Some columns are missing in Obtained Data. Check 2: In cleanAndSubsetDetailedData(wxdata, date, opt_temperature_columns, : Some columns are missing in Obtained Data. Check 3: In getWeatherForDate("IMUU011F4", "2015-01-01", "2017-09-02", station_type = "id", : Station data not available. IMUU011F4 2015-01-01 to 2017-09-02 – M.Yen Nov 27 '17 at 02:26
  • Your original question was about `getWeatherForDate()`. Why are you complicating the issue by adding error messages from other functions? Also, why did you change the end date from 2016-12-31 when you originally asked about getting 2 years of data and the start date was 2015-01-01? – Len Greski Nov 27 '17 at 03:40