I am trying to perform IDW interpolation on precipitation data in R using the 'gstat' and 'raster' packages. However, I am having trouble creating raster files from my interpolated data using the 'writeGDAL' function from the 'rgdal' package.
Here is the code I am using:
#Load libraries
library(sp)
library(gstat)
library(rgdal)
#Load data
data <- read.csv("20140516.csv")
#Convert to spatial points
coordinates(data) <- c("Long", "Lat")
#Create a grid
grid <- readOGR("1000_grid.shp")
#Convert CRS
proj4string(data)\<-proj4string(grid)
data <- spTransform(data, crs(grid))
#Perform IDW interpolation
idw <- gstat::idw(formula = Precipitation \~ 1, locations = data, newdata = grid)
#Write raster to file
writeGDAL(idw, "precipitation_idw.tif")
When I try to run the code, I get the following error message:
Error in create2GDAL(dataset = dataset, drivername = drivername, type = type, : gridded(dataset) is not TRUE
Can anyone help me figure out what is causing this error and how to fix it?
Thank you in advance for your assistance.