Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/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-Rnoaa获取天气数据_R_Rnoaa - Fatal编程技术网

从R-Rnoaa获取天气数据

从R-Rnoaa获取天气数据,r,rnoaa,R,Rnoaa,我正在尝试从R的rnoaa获取一些天气数据。由于rnoaa只支持一年的提取,我尝试将一个循环组合起来以获得几年。使用map函数更好吗 它返回一个空白列表 library(rnoaa) options(noaakey= "somekey") washington_weather <- getweather("GHCND:USW00024234") getweather <- function(stid) { wtr<-0 for (i

我正在尝试从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)
}
库(rnoaa)
选项(noaakey=“somekey”)

华盛顿天气来自
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)

getweatherrnoaa包允许您组合使用该包获得的多个ncdc对象

如果使用
ncdc\u 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)
x