0

I have thousands of coordinates in degree decimal minutes that I need to convert into decimal only. Similar questions have been asked, but have different formats of minutes and seconds so any answers would be really appreciated.

My data looks like:

lat 413190, lon 1710130 E

lat 425510, lon 1762550 W

...

To explain the first line, lat: 41 degrees, 31.90 minutes, lon: 171 degrees, 01.30 minutes East.

Thanks in advance!

TJeff
  • 29
  • 8

1 Answers1

1

Assuming that your data set in columns, lat is N, and lon is a character string: Here's some code I modified from https://stackoverflow.com/a/37194428/16247331

library(measurements)
df <- data.frame(lat = c(413190, 425510), lon = c("1710130 E", "1762550 W")) #assuming lat is N
df$nlat <- paste0(substr(df$lat, 1, nchar(df$lat) - 4), " ", substr(df$lat, nchar(df$lat) - 3, nchar(df$lat) - 2), ".", substr(df$lat, nchar(df$lat) - 1, nchar(df$lat)))
df$nlon <- paste0(substr(df$lon, 1, nchar(df$lon) - 6), " ", substr(df$lon, nchar(df$lon) - 5, nchar(df$lon) - 4), ".", substr(df$lon, nchar(df$lon) - 3, nchar(df$lon) - 2))
df$nlat <- as.numeric(measurements::conv_unit(df$nlat, from = 'deg_dec_min', to = 'dec_deg'))
df$nlon <- as.numeric(measurements::conv_unit(df$nlon, from = 'deg_dec_min', to = 'dec_deg'))
df$nlon[substr(df$lon, nchar(df$lon), nchar(df$lon)) == "W"] <- -1*df$nlon[substr(df$lon, nchar(df$lon), nchar(df$lon)) == "W"]

Result:

     lat       lon     nlat      nlon
1 413190 1710130 E 41.53167  171.0217
2 425510 1762550 W 42.91833 -176.4250

Not sure if there is a prettier way. Best of luck!

eyy
  • 72
  • 7
  • Hello, this seems like it will work, but I'm actually having trouble finding the package used 'measurements'. Is it only available on certain R versions? – TJeff Jul 14 '21 at 21:41
  • @TJeff hmm, I just used install.packages("measurements") with no problem. Here is the documentation: https://cran.r-project.org/web/packages/measurements/measurements.pdf – eyy Jul 14 '21 at 21:46
  • All good, think it was some temporary thing. I started a new R file and then it updated the previous one. Think it's something to do with R getting confused between shells or files or something. – TJeff Jul 14 '21 at 21:49
  • Thank you very much for this, it works well and I never would have been able to come up with it myself! Have a great day! – TJeff Jul 14 '21 at 22:09