-3

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!

Harmzy15
  • 487
  • 3
  • 6
  • 15
  • 4
    This isn't [reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) because we don't have any data to run it ourselves to verify. Follow the included link to see how to create a sample dataset for testing. Also, are you sure decimal places are being "lost"? R doesn't always print all the digits to the console yet they are still retained. It's unclear to me exactly what you think is wrong. Is there code you can run that shows one result when you expect a different one? – MrFlick May 08 '15 at 17:10
  • @MrFlick thank you for your response. Below is the summary from x.range and y.range. As you can see there is a difference between min and max. 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 However what I get is an error from coordinates(nuclides.grd) <- ~ x+y > gridded(nuclides.grd) <- TRUE saying grid parameters cannot be derived from a single point – Harmzy15 May 08 '15 at 17:18
  • 3
    Please edit the question and add the data in the question, not in the comments (unreadable as such). – kebs May 08 '15 at 17:36

1 Answers1

0

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.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • thanks a bunch. The 3.5 is the cell size I prefer. Is there another way to specify this property? – Harmzy15 May 11 '15 at 15:25
  • 1
    With your data, that's like asking me to bin the heights of people in my office into miles. Turns out everyone I work with is less than 1 mile tall, they're all in the same bin. According to your ranges, all your points are within .01, so they're all in the same bin of size 3.5. – Gregor Thomas May 11 '15 at 16:03
  • You either need data that's covers an approximately 350 times larger area to get it to a 2x2 grid of cell size 3.5, or you need to adjust your preference for the grid size. Maybe you can do 3.5 minutes instead of 3.5 degrees? – Gregor Thomas May 11 '15 at 16:05