Variogram matrix error

Hi everyone,
I'm trying to create a variogram using packages gstat and sp.
the script goes as follow :
reszn <- read_excel("C:/....../residuszn.xlsx")
head(reszn)
coordinates(reszn)=~x+y
class(reszn)
vario_reszn<-variogram(reszn~1, data=reszn)
plot(vario_reszn)

the class(reszn) gives me this output :
[1] "SpatialPointsDataFrame"
attr(,"package")
[1] "sp"
so it does not seem to have any issue at this point
but when it comes to
vario_reszn<-variogram(reszn~1, data=reszn)

I have this error popping out
Error in model.frame.default(terms(formula), as(data, "data.frame"), na.action = na.fail) :
the object isn't a matrix

I tried to convert my spatialized dataframe as a matrix using
mreszn<-data.matrix(reszn,rownames.force = FALSE)
and I have this error :
Error in as.vector(data) :
No method to automaticaly convert this S4 class into vector.

So I don't know what to do anymore... Any help would be very welcome :smile:
thanks :slight_smile:
PS : error messages have been translated from french
PPS: I don't have any NA in my data frame.

Hi,

Could you try and create a reprex of this issue so we can better understand and debug it? A reprex consists of the minimal code and data needed to recreate the issue/question you're having. You can find instructions how to build and share one here:

Thanks,
PJ

here it is
df<-data.frame(
x = c(142440.41,142961.81,143267.33,
143236,143665.82,142813.32),
y = c(167843.83,168237.34,168085.43,
168213.28,169220.81,169094.54),
residualZn = c(0.488249621071484,
-0.172036591379516,0.117844597600621,
0.617521750723651,-0.286365623586011,
-0.0886741489196016)
)

library(gstat)
#> Warning: le package 'gstat' a été compilé avec la version R 3.6.3
library(sp)
#> Warning: le package 'sp' a été compilé avec la version R 3.6.3

head(df)
#> x y residualZn
#> 1 142440.4 167843.8 0.48824962
#> 2 142961.8 168237.3 -0.17203659
#> 3 143267.3 168085.4 0.11784460
#> 4 143236.0 168213.3 0.61752175
#> 5 143665.8 169220.8 -0.28636562
#> 6 142813.3 169094.5 -0.08867415
class(df)
#> [1] "data.frame"
coordinates(df)=~x+y
class(df)
#> [1] "SpatialPointsDataFrame"
#> attr(,"package")
#> [1] "sp"

vario_df<-variogram(df~1, data=df)
#> Error in model.frame.default(terms(formula), as(data, "data.frame"), na.action = na.fail): l'objet n'est pas une matrice
plot(vario_df)
#> Error in plot(vario_df): objet 'vario_df' introuvable
mdf<-data.matrix(df,rownames.force = NA)
#> Error in as.vector(data): pas de méthode pour convertir automatiquement cette classe S4 en vecteur

at least I learnt how to do a reprex thanks to you :smile:

I found the issue
vario_df<-variogram(df~1, data=df)
should be
vario_df<-variogram(residualZn~1, data=df)

I was calling the dataframe instead of calling the data vector.
Doing the reprex made it clearer. Thanks

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.