Create spatial objects in R useful for coordinates() and spsample()

2.2k Views Asked by At

I'm trying to use this code, adapted from dataset meuse

data<-list(var1,var2,x,y)

coordinates(data)=~x+y

grid = spsample(data, type = "regular", cellsize = c(0.05,0.05))

vt <- variogram(var1 ~ var2,data=data)

vt.fit <- fit.variogram(vt, vgm(0.2, "Sph", 800, 0.05))

gstatobj <- gstat(id = 'var1', formula = var1 ~ var2, model=vt.fit, set = list(gls=1))

My goal is creating a grid, like meuse.grid. But coordinates doesn't work... list isn't the right command.

What shall I use? Is correct the way I'm using to create the grid?

1

There are 1 best solutions below

0
On BEST ANSWER

the following reproducible example shows jlhoward's comment is right, and Darko's reply is wrong:

library(gstat)
var1 = 1:3; var2 = 1:3; x = 1:3; y = 1:3
data<-list(var1,var2,x,y)
coordinates(data) = ~x+y
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘coordinates<-’ for signature ‘"list"’
data<-data.frame(var1,var2,x,y)
coordinates(data) = ~x+y
class(data)
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"

you may have been confused by doing this again, which would give:

coordinates(data) = ~x+y
Error in `coordinates<-`(`*tmp*`, value = ~x + y) : 
  setting coordinates cannot be done on Spatial objects, where they have already been set

but leaves the existing (and correct) data in tact.