2

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?

Darko
  • 1,448
  • 4
  • 27
  • 44
  • The way you've created it, `data` is not a named list, it's just a list with 4 elements. Try `data <- data.frame(var1,var2,x,y); coordinates(data) <- ~x+y`. – jlhoward Dec 15 '14 at 18:25
  • I also tried this, but it said that `data.frame` is not the correct type for `coordinates`. It needs something like a spatial object... – Darko Dec 16 '14 at 07:14

1 Answers1

1

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.

Edzer Pebesma
  • 3,814
  • 16
  • 26