char2dms.Rd
These two helper functions convert character vectors and decimal degree vectors to the DMS-class representation of degrees, minutes, and decimal seconds. "DMS" objects cannot contain NAs.
char2dms(from, chd = "d", chm = "'", chs = "\"")
dd2dms(dd, NS = FALSE)
coerce a "DMS"
object to a "numeric"
vector
coerce a "DMS"
object to a "character"
vector (the as.character.DMS
S3 method is also available)
In char2dms, the input data vector should use a regular format, such as that used in the PROJ.4 library, with a trailing capital (NSWE) indicating compass direction.
Both functions return a "DMS" object.
data(state)
str(state.center$y)
#> num [1:50] 32.6 49.2 34.2 34.7 36.5 ...
stateN <- dd2dms(state.center$y, NS=TRUE)
str(attributes(stateN))
#> List of 6
#> $ WS : logi [1:50] FALSE FALSE FALSE FALSE FALSE FALSE ...
#> $ deg : int [1:50] 32 49 34 34 36 38 41 38 27 32 ...
#> $ min : int [1:50] 35 15 13 44 32 40 35 40 52 19 ...
#> $ sec : num [1:50] 24.36 0 9.12 0.96 2.76 ...
#> $ NS : logi TRUE
#> $ class: chr "DMS"
#> ..- attr(*, "package")= chr "sp"
ch.stateN <- as.character(stateN)
str(ch.stateN)
#> chr [1:50] "32d35'24.36\"N" "49d15'N" "34d13'9.12\"N" "34d44'0.96\"N" ...
stateNa <- char2dms(ch.stateN)
str(attributes(stateNa))
#> List of 6
#> $ WS : logi [1:50] FALSE FALSE FALSE FALSE FALSE FALSE ...
#> $ deg : int [1:50] 32 49 34 34 36 38 41 38 27 32 ...
#> $ min : int [1:50] 35 15 13 44 32 40 35 40 52 19 ...
#> $ sec : num [1:50] 24.36 0 9.12 0.96 2.76 ...
#> $ NS : logi TRUE
#> $ class: chr "DMS"
#> ..- attr(*, "package")= chr "sp"
ch.stateN <- as(stateN, "character")
str(ch.stateN)
#> chr [1:50] "32d35'24.36\"N" "49d15'N" "34d13'9.12\"N" "34d44'0.96\"N" ...
stateNa <- char2dms(ch.stateN)
str(attributes(stateNa))
#> List of 6
#> $ WS : logi [1:50] FALSE FALSE FALSE FALSE FALSE FALSE ...
#> $ deg : int [1:50] 32 49 34 34 36 38 41 38 27 32 ...
#> $ min : int [1:50] 35 15 13 44 32 40 35 40 52 19 ...
#> $ sec : num [1:50] 24.36 0 9.12 0.96 2.76 ...
#> $ NS : logi TRUE
#> $ class: chr "DMS"
#> ..- attr(*, "package")= chr "sp"