From Decimal Minutes to Decimal Degrees for your analysis

R
code
function
Author

Federico Maioli

Published

September 18, 2022

It might happen that Latitude and Longitude of your spatially referenced data are formatted like this c(4530.12, 1310.02), i.e. as Decimal Minutes (D° M.M’). This is likely not a friendly a format for your analysis. However, with this function you can convert it to the more R friendly format Decimal Degrees (D.D°). The function was provided me by Patrik Börjesson (SLU Aqua).

format.position <- function(x){
  sign.x <- sign(x)
  x <- abs(x)
  x <- ifelse(nchar(x)==3, paste("0",x,sep=""), x)
  x <- ifelse(nchar(x)==2, paste("00",x,sep=""), x)
  x <- ifelse(nchar(x)==1, paste("000",x,sep=""), x)
  dec.x <- as.numeric(paste(substring(x,1,2)))+as.numeric(paste(substring(x,3,4)))/60
  dec.x <- sign.x*dec.x
}

Reproducible example

lat.dm.m = 4530.12
lat.d.d = format.position(lat.dm.m)
lat.d.d
[1] 45.5