Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.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 Meteo_pull_监测器,用于按纬度/经度计算的平均气候数据_R_Rnoaa - Fatal编程技术网

R Meteo_pull_监测器,用于按纬度/经度计算的平均气候数据

R Meteo_pull_监测器,用于按纬度/经度计算的平均气候数据,r,rnoaa,R,Rnoaa,我有一个纬度、经度、开始年份和结束年份的数据框架。我想要那个时期每个地点的平均降雨量 现在,我可以一次为一个位置获取此信息,但我希望为多个位置自动执行以下操作: 以下是一些先决条件: #library(xts) #library(rnoaa) #options(noaakey = "...") # https://ropensci.org/blog/2014/03/13/rnoaa/ says how to get a API key #station_data <- ghcnd_stat

我有一个纬度、经度、开始年份和结束年份的数据框架。我想要那个时期每个地点的平均降雨量

现在,我可以一次为一个位置获取此信息,但我希望为多个位置自动执行以下操作:

以下是一些先决条件:

#library(xts)
#library(rnoaa)
#options(noaakey = "...") # https://ropensci.org/blog/2014/03/13/rnoaa/ says how to get a API key
#station_data <- ghcnd_stations() # Takes a while to run
statenv <- new.env()
lat_lon_df<-structure(list(lat = c(41.1620277777778, 44.483333, 44.066667
), long = c(-96.4115, -92.533333, -93.5), yrmin = c(2001L, 1983L, 
                                                    1982L), yrmax = c(2010L, 1990L, 1992L), id = c("ithaca", "haycreek", 
                                                                                                   "waseca")), class = "data.frame", row.names = c(1389L, 1395L, 
                                                                                                                                                   1403L))
一定有办法把这件事弄清楚
只需按
lat_long_df
行重复,即
lat_long_df[1,]
,然后
lat_long_df[2,]
,最后
lat_long_df[3,]
一种方法是
lat lon_df
行上应用自定义函数

以下是一个例子:

library(xts)
library(rnoaa)
设置API密钥

#options(noaakey = "...") # https://ropensci.org/blog/2014/03/13/rnoaa/ says how to get a API key

station_data <- ghcnd_stations() #meta-information about all available GHCND weather stations
请注意,当
yrmin
yrmax
不改变时,只需使用
lat\u lon\u df上的
meteo\u近站
即可获得所需信息

您还可以将其定义为命名函数

get_mean_precip <- function(x){
  min_year <- x[3]
  max_year <- x[4]
  ll_df <- data.frame(lat = as.numeric(x[1]),
                      long = as.numeric(x[2]),
                      id = x[5])
  nearby_station <- rnoaa::meteo_nearby_stations(lat_lon_df = ll_df,
                                                 lat_colname = "lat",
                                                 lon_colname = "long",
                                                 station_data = station_data,
                                                 radius = 50,
                                                 year_min = min_year,
                                                 year_max = max_year,
                                                 limit=1,
                                                 var = "PRCP")
  res <- lapply(nearby_station, function(y) {
    res <- rnoaa::meteo_pull_monitors(y[1]$id)
  }
  )
  ll <- xts::xts(res[[1]]$prcp, order.by=res[[1]]$date)
  x <- paste0(min_year <- x[3],"/",max_year) 
  mean(xts::apply.yearly(na.omit(ll[x]),sum))/10
}
get\u mean\u precip
#options(noaakey = "...") # https://ropensci.org/blog/2014/03/13/rnoaa/ says how to get a API key

station_data <- ghcnd_stations() #meta-information about all available GHCND weather stations
out <- apply(lat_lon_df, 1, function(x){
  min_year <- x[3] #extract the needed values min_year, max_year and ll_df
  max_year <- x[4] 
  ll_df <- data.frame(lat = as.numeric(x[1]),
                      long = as.numeric(x[2]),
                      id = x[5])
  nearby_station <- meteo_nearby_stations(lat_lon_df = ll_df,
                                          lat_colname = "lat",
                                          lon_colname = "long",
                                          station_data = station_data,
                                          radius = 50,
                                          year_min = min_year,
                                          year_max = max_year,
                                          limit=1,
                                          var="PRCP")
  res <- lapply(nearby_station, function(y) {
    res <- meteo_pull_monitors(y[1]$id)
    }
    )
  ll <- xts(res[[1]]$prcp, order.by=res[[1]]$date)
  x <- paste0(min_year <- x[3],"/",max_year) 
  mean(xts::apply.yearly(na.omit(ll[x]),sum))/10
}
)

data.frame(lat_lon_df, precip = out)
#output
          lat      long yrmin yrmax       id   precip
1389 41.16203 -96.41150  2001  2010   ithaca 776.2300
1395 44.48333 -92.53333  1983  1990 haycreek 829.6500
1403 44.06667 -93.50000  1982  1992   waseca 894.6273
get_mean_precip <- function(x){
  min_year <- x[3]
  max_year <- x[4]
  ll_df <- data.frame(lat = as.numeric(x[1]),
                      long = as.numeric(x[2]),
                      id = x[5])
  nearby_station <- rnoaa::meteo_nearby_stations(lat_lon_df = ll_df,
                                                 lat_colname = "lat",
                                                 lon_colname = "long",
                                                 station_data = station_data,
                                                 radius = 50,
                                                 year_min = min_year,
                                                 year_max = max_year,
                                                 limit=1,
                                                 var = "PRCP")
  res <- lapply(nearby_station, function(y) {
    res <- rnoaa::meteo_pull_monitors(y[1]$id)
  }
  )
  ll <- xts::xts(res[[1]]$prcp, order.by=res[[1]]$date)
  x <- paste0(min_year <- x[3],"/",max_year) 
  mean(xts::apply.yearly(na.omit(ll[x]),sum))/10
}
out <- apply(lat_lon_df, 1, get_mean_precip)