【问题标题】:How to download and save multiple files from a single online source?如何从一个在线资源下载和保存多个文件?
【发布时间】:2021-11-01 08:44:04
【问题描述】:

我正在尝试使用 riem 包从多个机场下载天气数据。我有当前下载单个机场的天气并将其保存为 station_METAR.csv 的代码,其中 station 是每个单独的城市。我希望每个城市都有单独的文件。我创建了另一个 CSV 文件,我将它带入名为“站”的环境中,并计划使用它的值来重复该过程。

我需要为每个单独的城市迭代这个下载和保存过程。

我不一定有一个特定的问题,因为我是 R 编码的新手,并且正在努力解决如何解决这个循环。提前感谢您的帮助!

x <- "KJFK"
start_date <- "2020-01-01"
end_date <- "2020-01-04"

#Download Station List
stations <- read_excel("~/Desktop/R WD/Reference Files/stations.xlsx", 
                       col_names = FALSE)

#View(stations)

#Download Data
METAR <- riem_measures(station = x, date_start = start_date,date_end = end_date)

#Write a CSV file with the City Name
write.table(METAR, file=paste(x, "_METAR", sep=""))```

【问题讨论】:

    标签: r loops


    【解决方案1】:

    这使您可以遍历每个唯一的站点并为每个站点创建一个文件路径,然后将其写入该路径。

    start_date <- "2020-01-01"
    end_date <- "2020-01-04"
    
    #Download Station List
    stations <- read_excel("~/Desktop/R WD/Reference Files/stations.xlsx", 
                           col_names = FALSE)
    
    # Select first column and turn it into a character vector. I assume other columns dont exist or arent important. Only use unique values, to avoid duplicates. 
    stations = unique(as.character(stations[, 1]))
    
    # loop over stations
    for(station_name in stations) {
      
      #Download Data
      METAR <- riem_measures(station = station_name,
                             date_start = start_date,
                             date_end = end_date)
      
      # define filepath
      filepath = paste0(station_name, "_METAR.csv")
      
      #Write a CSV file with the City Name
      write.table(METAR, file=filepath)
      
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多