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 dat
With
structure(list(longitude = c(128.6979, 153.0046, 104.3261, 124.9019,
126.7328, 153.2439, 142.8673, 152.689), latitude = c(-7.4197,
-4.7089, -6.7541, 4.7817, 2.1643, -5.65, 23.3882, -5.571)), .Names = c("longitude", "latitude"), class = "data.frame", row.names = c(NA, -8L))
To convert to SpatialPointsDataFrame
coordinates(df) <- cbind(df$longitude , df$latitude)
As pointed out by @jazzurro you will probably need to assign a CRS to your spatial object.
proj4string(df) = CRS("+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0")
the reverse process SpatialPointsDataFrame to original df
df <- data.frame(longitude = coordinates(df)[,1], latitude = coordinates(df)[,2])