Trying to interpolate a set of data using IDW. I am able to bring in the csv and convert it to a spatial points data frame. My column headings are lat and lon instead of x and y.
The coordinates are very minimal in distance due to the csv being representative of a farmers field. As a result the code believes that the minimum and maximum coordinates for x and y are the same because of the as.numeric
dropping the decimal points and rounding the coordinates.
That is where the code errors out because when I try to run the expand.grid
it sees no difference in x.range[1]
and x.range[2]
.
Does anyone see something that could be fixed? Is there a way to carry more decimal points in my coordinates to make sure there is the difference between x.range[1]
and x.range[2]
and similarly for y.range
?
path<- setwd()
library(ggplot2)
library(gstat)
library(sp)
library(maptools)
#Checking rgeos availability: TRUE
nuclides <- read.csv("fieldnuclides.csv")
#convert this basic data frame into a spatial points data frame
coordinates(nuclides) = ~ lon + lat
## Create a grid from the values in your points dataframe
## first get the range in data
x.range <- as.numeric(range(nuclides@coords[,1]))
y.range <- as.numeric(range(nuclides@coords[,2]))
nuclides.grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 3.5), y = seq(from = y.range[1], to = y.range[2], by = 3.5))
#
# ## convert grid to SpatialPixel class
coordinates(nuclides.grd) <- ~ x+y
gridded(nuclides.grd) <- TRUE
#
my x.range
and y.range
summaries are below
summary(x.range)
Min. 1st Qu. Median Mean 3rd Qu. Max.
-80.65 -80.64 -80.64 -80.64 -80.64 -80.63
summary(y.range)
Min. 1st Qu. Median Mean 3rd Qu. Max.
43.42 43.43 43.43 43.43 43.43 43.43
So there is a difference but coordinates(nuclides.grd)
gives me the error below stating its a single point, however there is a difference according to the summary
coordinates(nuclides.grd) <- ~ x+y
gridded(nuclides.grd) <- TRUE
Error in points2grid(points, tolerance, round) :
cannot derive grid parameters from a single point!