rgstat

Loop variable IDW gstat R


Is there a way to loop over variables inside an SpatialPointsDataFrame object in R using IDW interpolation of gstat package ?

I mean something like this

for (day in list_days) {
  P.idw <- gstat::idw(day~1, P, newdata=grd, idp=2.0)
  r       <- raster(P.idw)
  plot(r)
}

Edit: Because that structure gives me error:

Error in predict.gstat(g, newdata = newdata, block = block, nsim = nsim,  : 
  too many spatial dimensions: 58
In addition: Warning message:
In predict.gstat(g, newdata = newdata, block = block, nsim = nsim,  :
NAs introduced by coercion

But if I write directly the name of the variable it works fine:

P.idw <- gstat::idw(X2018.01.14~1, P, newdata=grd, idp=2.0)

Solution

  • Rather than day~1 you can do as.formula(paste(day, "~ 1"))