0

I am trying to create spatial points variables so R recognises the columns as geographic coordinates. The code and the error message are shown below. I think it could be related to the fact that my coordinates are Easting & Northing Coordinates WGS 84 instead of the usual long/lat? Any help would be greatly appreciated.

elephant.all.sp<-SpatialPoints(elephant[c(GPS.E,GPS.S)])
Error in h(simpleError(msg, call)) : error in evaluating the argument 'obj' in selecting a method for function 'coordinates': undefined columns selected

elephant.all.sp<-SpatialPoints(elephant[c(GPS.S,GPS.E),])
Error in .local(obj, ...) : cannot derive coordinates from non-numeric matrix

Bett21
  • 23
  • 1
  • 5
  • 1
    Hi, can you provide an example of your data? See [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for some advice on creating reproducible examples. – Lief Esbenshade Jan 05 '21 at 06:13

1 Answers1

0

Eastings and Northings should not be a problem, R should understand every format. It seems you have two errors. In the first line your are missing quotes:

elephant.all.sp<-SpatialPoints(elephant[c("GPS.E","GPS.S")])

Using the bracket notation to extract from a data.frame requires a text vector. Unquoted only works when using $, e.g. elephant$GPS.E.

On the second line you are getting a coercion error. R is understanding that you are giving SpatialPoints() a matrix filled with text, which it does not understand how it could be turned into numbers and then coordinates. This is most likely because you calling the rows "GPS.E" and "GPS.S" and not columns. This is because you placed the c("GPS.E","GPS.S") before the comma in the middle of the brackets. The correct form should be:

elephant.all.sp<-SpatialPoints(elephant[,c("GPS.S","GPS.E")])

Note that when there are no commas in the brackets, R treats the value given as column indexes (in data.frames), which makes the two lines in my answer equivalent.

JMenezes
  • 1,004
  • 1
  • 6
  • 13