Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.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/9/loops/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 函数运行在不同日期循环的IDW模型_R_Loops_Spatial - Fatal编程技术网

R 函数运行在不同日期循环的IDW模型

R 函数运行在不同日期循环的IDW模型,r,loops,spatial,R,Loops,Spatial,我正在尝试为一个地点运行一个IDW模型,其中包含一年的每日降雨数据。我有一个函数,将返回一天的预测,但我想给它很多天,并得到每天的预测。我被困在函数内的循环中,无法循环日期 下面是我正在处理的示例数据 date <- c("2015-04-02", "2015-04-02", "2015-04-02", "2015-04-03", "2015-04-03", "2015-04-03", "2015-04-04", "2015-04-04", "2015-04-04") longitude

我正在尝试为一个地点运行一个IDW模型,其中包含一年的每日降雨数据。我有一个函数,将返回一天的预测,但我想给它很多天,并得到每天的预测。我被困在函数内的循环中,无法循环日期

下面是我正在处理的示例数据

date <- c("2015-04-02", "2015-04-02", "2015-04-02", "2015-04-03", "2015-04-03", "2015-04-03", "2015-04-04", "2015-04-04", "2015-04-04")
longitude <- c(-95.47188, -95.47188, -95.47188, -95.47188, -95.47188, -95.47188, -95.47188, -95.47188, -95.47188)
latitude <- c(40.73238, 40.73238, 40.73238, 40.73238, 40.73238, 40.73238, 40.73238, 40.73238, 40.73238)
station.lat <- c(40.606, 41.117, 39.774, 40.606, 41.117, 39.774, 40.606, 41.117, 39.774)
station.long <- c(-95.864, -95.917, -94.923, -95.864, -95.917, -94.923, -95.864, -95.917, -94.923)
precip <- c(0.44, 1.00, 0.51, 0.02, 0.03, 0.10, 0.00, 0.00, 0.02)

test <- data.frame(date, longitude, latitude, station.lat, station.long, precip)
names(test) <- c("date", "longitude", "latitude", "station.lat", "station.long", "precip")
日期
library(dplyr)
library(sp)
library(gstat)

iwd <- function(x){   
      spatial.loc <- x[,c(4,5,6)]  #makes station.lat, station.long and percip spatial
      coordinates(spatial.loc) = ~station_long+station_lat    

      predict.loc <- x[1,c(2,3)]   #Makes latitude and longitude spatial for and sets it a the grid for the prediction location
      predict.loc.point <- SpatialPoints(predict.loc,    
      proj4string=CRS(as.character(NA)), bbox = NULL)   

      x <- idw(total.precipitation~1, spatial.loc, predict.loc.point, idp=4, debug.level = 0)
      x <- as.vector(x@data$var1.pred)
    return(x) 
  }


iwd(test) #run the function if I filter only the locations for 2015-04-02

[1] 0.547994