r语言 - 如何从一个在线源下载和保存多个文件?



我正在尝试使用riem包从多个机场下载天气数据。我的代码当前下载单个机场的天气,并将其保存为station_METAR.csv,其中站点是每个单独的城市。我希望有每个城市的个人档案。我创建了另一个CSV文件并将其导入到环境中标题为" stations "并计划使用这个值来重复这个过程。

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

我不一定有一个具体的问题,因为我是新的编码在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)

}

最新更新