2017-11-26 6 views
0

R의 새로운 기능이므로 지식 부족을 용서하십시오. 나는 wunderground를 사용하여 R에서 특정 시간 간격 동안 날씨 데이터를 가져갈 필요가있다. 그것에 대한 명령이 있지만 시간 단위가 아닙니다. 2015 년에서 2017 년까지의 시간별 데이터를 가져와야합니다. 따라서 for 루프를 작성해야합니다. 특정 기상 조건에 대한 단일 날짜에 사용되는 코드는 다음과 같습니다getWeatherForDate가있는 R의 루프의 경우

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

그래서, 내가 2 년 동안 하나의 파일이 2 년 모든 일이 코드를 사용해야합니다.

for 루프를 쓸 수 없습니다.

아무도 도와 줄 수 있습니까?

감사합니다.

답변

0

2 년 동안 단일 기상 관측소를 당기는 데 필요한 루프가 없습니다. 대신 end_date= 인수를 사용하십시오. 당신은 2015 년 1 월 1 일부로 시작하고 있기 때문에, 종료 날짜는 다운로드 프로세스를 시작으로 R 콘솔에 많은 양의 데이터를 기록 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)) 

년 12 월 31 일이 될 것입니다 다음

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) 
    . 
    . 
    . 

결과 데이터 프레임은 다음과 같습니다. 여러 기상 관측소의 날씨를 얻으려면

> 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 
> 

, 이것은 apply() 기능을 수행 할 수 있습니다. IMUU011F4 기상 관측소는 2 년 데이터 요청에 대해 203,000 행 이상의 출력을 생성했기 때문에이 작업을 실행하는 데 몇 분이 소요됩니다.

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) 
+0

"데이터를 사용할 수 없음"이라고 표시되어 있습니다. 그러나 하루를 볼 때 데이터가 있습니다. –

+0

@ M.Yen - "데이터를 사용할 수 없음"에 대한 귀하의 의견과 관련하여 더 많은 컨텍스트가 필요합니다. –

+0

@ M.Yen - 기상 관측소 IMUU011F4에 대한 2016 년 데이터가 [8 월 24 일] 부분적으로 누락 된 것 같습니다 (https://www.wunderground.com/personal-weather-station/dashboard?ID=IMUU011F4#history/s20160824/e20160824/mdaily) 및 [9 월 11 일] (https://www.wunderground.com/personal-weather-station/dashboard?ID=IMUU011F4#history/s20160911/e20160911/mdaily) 및 8 월 25 일부터 9 월까지 완전히 사라졌습니다. 로그에 기록 된 내용에 따라 10 번째. 며칠 동안 7 월 17 일과 8 월 17 일에 NULL 값이 포함 된 것으로보고되었습니다. –