Search code examples
rcoordinatesgisdata-conversion

How to convert degrees decimal minutes, to decimal degrees


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!


Solution

  • 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!