Harmzy15 Harmzy15 - 8 months ago 79
R Question

Inverse Distance Weighting in R

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

dropping the decimal points and rounding the coordinates.
That is where the code errors out because when I try to run the
it sees no difference in

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
and similarly for

path<- setwd()

#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

summaries are below

Min. 1st Qu. Median Mean 3rd Qu. Max.
-80.65 -80.64 -80.64 -80.64 -80.64 -80.63
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
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!


Well, based on your summaries the range of x is 0.02 and the range of y is 0.01. However, in your expand.grid call you ask for sequences from the min to the max with a step size of 3.5. So your seq calls will return 1 value each, and your expand.grid will be a single point.

I'd recommend replacing by = 3.5 with length.out = 10 in your seq calls. Then you'll get multiple values in the sequences and multiple points in our grid.

As a debugging technique, check your inputs! You said that the error was with coordinates(nuclides.grd) saying it's a single point. This is easy to verify! Check if nuclides.grd is a single point (yes, it is), so then look at where it's defined.