Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何将地下天气(地下天气)数据提取到csv文件中_R_Date_Append_Iteration_Wunderground - Fatal编程技术网

R 如何将地下天气(地下天气)数据提取到csv文件中

R 如何将地下天气(地下天气)数据提取到csv文件中,r,date,append,iteration,wunderground,R,Date,Append,Iteration,Wunderground,我试图通过对每个日期运行一个带有迭代[I]的for循环,将历史数据从weather underground下载到csv格式 为了加快运行时间,我将代码从2019年9月1日运行到今天(我实际上需要运行几年)。 我正在为url运行for循环: myurl[i] <- paste("https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=",station,"&day=",my_dates$day[i],"&am

我试图通过对每个日期运行一个带有迭代[I]的for循环,将历史数据从weather underground下载到csv格式

为了加快运行时间,我将代码从2019年9月1日运行到今天(我实际上需要运行几年)。 我正在为url运行for循环:

myurl[i] <- paste("https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=",station,"&day=",my_dates$day[i],"&month=",my_dates$month[i],"&year=",my_dates$year[i],"&graphspan=day&format=1", sep = "")
myurl[i]我已经整理好了

正如@AndrewGustar指出的,我需要将i合并到文件名中,我发现可以这样做

myfile[i] <- paste("NewportWeather",i,".csv")

myfile[i]问题可能是每次保存的文件名都相同。大概它应该取决于
i
@AndrewGustar是的,我确实这么认为,但不确定如何将i合并到文件名中。如果保存为单个文件,文件名上最好有每个日期。做得好。实际上,在循环中,您只需使用
myurl
myfile
(即,no
[i]
)-它们会在
i
每次增加时重置,并且只使用一次,因此您可以将它们作为单个值保留。
myfile[i] <- paste("NewportWeather",i,".csv")
start_date <- as.POSIXct("2019-09-01",tz="gmt")
end_date <-  as.POSIXct(Sys.Date(),tz="gmt")

dates <- seq.POSIXt(start_date,end_date,by="day")

my_dates<-as.integer(unlist(strsplit(as.character(dates),"-")))

my_dates<-array(my_dates,dim=c(3,length(my_dates)/3))

my_dates<-as.data.frame(t(my_dates), )

colnames(my_dates)<-c("year","month","day")

station <- "IISLEOFW4"


for(i in 1:nrow(my_dates)) {

  myurl[i] <- paste("https://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=",station,"&day=",my_dates$day[i],"&month=",my_dates$month[i],"&year=",my_dates$year[i],"&graphspan=day&format=1", sep = "")
  folder <- "D:\\WeatherData"
  myfile[i] <- paste("NewportWeather",i,".csv")
  download.file(url = myurl[i], file.path(folder, myfile[i], fsep = "\\" ))
}