I have a datasets of X and Y coordinates that are in packed DMS format and I want to transform them to decimal degrees. To do that I have to separate the degrees, minutes and seconds. It would be straightforward if all the numbers in the column had the same length, but some have 7 and some 8. The data set looks something like this:
coords$X <- c(100942.2, 67942.2, 229942.2, 239942.2, 52942.2)
I had the idea to use an if else
statement to check the length of each number using this code:
if (nchar(coords$X == 8)){0
coords$degrees.x <- as.numeric(str_sub(coords$X, 1, 2))
coords$min.x <- as.numeric(substr(coords$X, 3, 4))
coords$sec.x <- as.numeric(substr(coords$XC, 5, 7))
} else {
coords$degrees.x <- as.numeric(str_sub(coords$X, 1, 1))
coords$min.x <- as.numeric(substr(coords$X, 2, 3))
coords$sec.x <- as.numeric(substr(coords$X, 4, 5))
}
But I am getting an error:
Error in if (nchar(coords$XCoordinatePlot == 8)) { :
the condition has length > 1
Is there a way to make this work?