【问题标题】:For loop in R with getWeatherForDateR中的for循环与getWeatherForDate
【发布时间】:2017-11-26 13:07:28
【问题描述】:

R 中的新内容,所以请原谅缺乏知识。我需要使用 wunderground 在 R 中获取特定时间间隔的天气数据。有一个命令,但不是每小时一次。我需要获取 2015 年到 2017 年的每小时数据。所以,我需要编写一个 for 循环。用于特定天气条件的单个日期的代码是:

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 循环。

谁能帮帮我。

谢谢。

【问题讨论】:

    标签: r for-loop wunderground


    【解决方案1】:

    2 年内无需循环即可拉动一个气象站。相反,请使用 end_date= 参数。由于您从 2015 年 1 月 1 日开始,因此结束日期为 2016 年 12 月 31 日。

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

    下载过程向R控制台写入大量数据,从以下开始:

    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)
    

    【讨论】:

    • 它说“数据不可用”。但是当我看一天的时候,有一个数据。
    • @M.Yen - 我们需要更多关于您对“数据不可用”的评论的背景信息。
    • @M.Yen - 根据所写的内容,August 24thSeptember 11th 的气象站 IMUU011F4 的 2016 年数据似乎部分缺失,而 8 月 25 日至 9 月 10 日的数据则完全缺失到日志。还有几天报告嵌入了 NULL 值:7 月 17 日和 8 月 17 日。
    • 它说“警告消息:1:在 cleanAndSubsetDetailedData(wxdata, date, opt_temperature_columns, : 获取的数据中缺少某些列。检查”即使缺少单元格,我也需要提供数据。有什么方法可以获取 NULL 或缺失单元格的数据?
    • @M.Yen - 你能用我提供的答案下载数据吗?如果是这样,请接受答案。发布另一个关于下载数据后如何清理数据的问题。
    猜你喜欢
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    • 1970-01-01
    • 2020-07-10
    • 2022-10-20
    • 2017-11-28
    相关资源
    最近更新 更多