Mapping inR - Help with Coordinates

Greeting all.
I have problem with nt showing my coordinates. Do i make something wrong?
Error problem:
Error in .local(obj, ...) :
cannot derive coordinates from non-numeric matrix
Thanks in advance

My guess is that there's some non-numeric data in the X or Y column. Maybe check those X and Y columns to make sure that there isn't any weird entries like character values or letters? Or perhaps missing values?

1 Like

Thank you for the quick answer. As you can see there are only numbers. Do you have any idea of what is happening?

The columns in a dataframe can still be loaded as character, despite it only containing numbers. This possible and dependent on how the data was loaded into R.
Try commands to figure out what class those two columns are:
class(df_sel$X)
class(df_sel$Y)

If they are 'character', you can convert them to a numeric class by using these commands:
df_sel$X <- as.numeric(df_sel$X)
df_sel$Y <- as.numeric(df_sel$Y)

You can read more about classes here:
https://resbaz.github.io/2014-r-materials/lessons/01-intro_r/data-structures.html

@Daikoro Thank you very much for your help. But i still have some issues. in the same data i write this code:
setwd("D:/R/WorkDir")
getwd()
install.packages(c("gstat","sp"))
library(gstat)
library(sp)
data <- read.csv("Dataa.csv", sep = ";")
head(data)

class(data$X)
class(data$Y)

data$X <- as.numeric(as.character(data$X)
data$Y <- as.numeric(as.character(data$Y)

f_spdf = sp::SpatialPointsDataFrame(coords = cbind(data$X,data$Y), data = data, proj4string = sp::CRS(projargs = "+init=epsg2100"))
var= gstat::variogram(object = data$CLAY~1, locations = f_spdf)
plot(var)

But i got these errors:

class(data$X)
[1] "character"
class(data$Y)
[1] "character"
data$X <- as.numeric(as.character(data$X)

  • data$Y <- as.numeric(as.character(data$Y)
    Error: unexpected symbol in:
    "data$X <- as.numeric(as.character(data$X)
    data"

f_spdf = sp::SpatialPointsDataFrame(coords = cbind(data$X,data$Y), data = data, proj4string = sp::CRS(projargs = "+init=epsg2100"))
Error in .local(obj, ...) :
cannot derive coordinates from non-numeric matrix
var= gstat::variogram(object = data$CLAY~1, locations = f_spdf)
Error in variogram.formula(object = data$CLAY ~ 1, locations = f_spdf) :
object 'f_spdf' not found

Do you have any idea why it is happening?

Yes, you're missing a closing bracket ')' at the end. I don't think you'll need to specify as.character again, but it's not wrong either. So this should work:
data$X <- as.numeric(data$X)
data$Y <- as.numeric(data$Y)

if you do need the as.character function use this:
data$X <- as.numeric(as.character(data$X))
data$Y <- as.numeric(as.character(data$Y))

notice that you need two closing brackets, one for each function.

@DimitrisK Did my last reply solve your issue? If so, please accept as an answer to help the rest of the community find solutions faster. Thank you!

Hello!

It helped but a bit cause i still have some issues.