r - 如何从一个在线资源下载和保存多个文件?
问题描述
我正在尝试使用 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=""))```
解决方案
这使您可以遍历每个唯一的站点并为每个站点创建一个文件路径,然后将其写入该路径。
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)
}
推荐阅读
- ssl-certificate - 如何配置自定义域可以加密关键云代工厂的证书
- oracle - 无法调用在 PL/SQL 包中定义的函数
- java - 导航抽屉 onNavigationItemSelected() 没有响应
- swift - 使用并发 for 循环填充结构数组会导致 malloc 错误
- java - Android:上传到 Google 云端硬盘?
- python - 在每个单元格中创建具有相同点数的 2D Hist
- javascript - 如何制作文本视差
- shell - 管道命令并使用命令重定向输出
- javascript - 如何将事件附加到控件
- python - python-聊天客户端中的输入问题(线程)