我正在尝试从R的rnoaa获取一些天气数据,因为rnoaa只支持一年的提取,所以我尝试将一个循环放在一起,以获得几年的提取。使用map函数更好吗?
它返回一个空白列表..
library(rnoaa)
options(noaakey= "somekey")
washington_weather <- getweather("GHCND:USW00024234")
getweather <- function(stid) {
wtr<-0
for (i in 2009:2017) {
start_date <- paste0(i, "-01-01")
end_date <- paste0(i, "-12-31")
j<- i -2008
wtr[j]$tbl <- ncdc(datasetid='GHCND', stationid=stid, startdate = start_date, enddate = end_date)
}
return(wtr)
}
fahrenheit_to_celsius <- function(temp_F) {
temp_C <- (temp_F - 32) * 5 / 9
return(temp_C)
}发布于 2020-08-08 02:17:25
rnoaa包允许您组合使用该包获得的多个ncdc对象。
如果使用ncdc_combine()函数,则可以合并您创建的多个对象。
例如:
x <- ncdc(datasetid= "GHNCD", stationid=stid, startdate = start_date, enddate = end_date)
y <- ncdc(datasetid= "GHNCD", stationid=stid, startdate = start_date1, enddate = end_date1)
z <- ncdc_combine(x,y)这将合并您的两个ncdc对象,只要您将其分解为每个ncdc对象不到一年。
发布于 2020-06-27 02:39:02
ncdc函数的返回值是一个列表。理想情况下,您只希望返回列表中的数据部分。
在这个脚本中,我下载了每年的数据,并将数据部分信息保存在一个列表中。然后,可以使用data.frames列表进行额外分析,或者将所有数据帧绑定到一个大型数据帧中。
getweather <- function(stid) {
wtr<-list() # create an empty list
for (i in 2009:2011) {
start_date <- paste0(i, "-01-01")
end_date <- paste0(i, "-12-31")
#save data portion to the list (elements named for the year
wtr[[as.character(i)]] <- ncdc(datasetid='GHCND', stationid=stid, startdate = start_date, enddate = end_date)$data
}
#return the full list of data frames
return(wtr)
}
washington_weather <- getweather("GHCND:USW00024234")
#bind the dataframes in the list together into one large dataframe
dplyr::bind_rows(washington_weather)https://stackoverflow.com/questions/62600760
复制相似问题