I have been working on earthquake data that has lat long values, and I want to convert those lat long values to spatial coordinates.
Suppose I have the following data set df
:
longitude latitude
128.6979 -7.4197
153.0046 -4.7089
104.3261 -6.7541
124.9019 4.7817
126.7328 2.1643
153.2439 -5.6500
142.8673 23.3882
152.6890 -5.5710
I want to convert it into spatial points. Something like this:
lon lat
[1,] 2579408.24 1079721.15
[2,] 2579333.69 1079729.18
[3,] 2579263.65 1079770.55
[4,] 2579928.04 1080028.46
[5,] 2579763.65 1079868.92
[6,] 2579698.00 1079767.97
I used the following code:
library(sp)
df.sp<-df
coordinates(df.sp)<-~x+y
But I receive the following error:
Error in `[.data.frame`(object, , -coord.numbers, drop = FALSE) :
undefined columns selected
x
andy
not being defined. In this example, your x and y aredf$longitude
anddf$latitude
. As such, one solution is to run:coordinates(df.sp)<-~ df$longitude + df$latitude
– Abroad