Convert to decimal degrees using birk library in R -


i using library sp suggested here , here convert degree-minute-second (dms) representation decimal degrees(dd).

if try following code using sp, result:

loc$lat<-sp::char2dms(loc$lat) 

error in rep(value, length.out = nrows) : attempt replicate object of type 's4'

a sample of data follows:

    c("39d47m01s n", "15d38m08s n", "12d45m01s n", "13d17m25s n",      "36d34m29s n", "24d46m34s s", "11d52m39s s", "39d47m01s n", "30d52m34s n",      "34d59m47s s") 

i need convert dms dd want calculate haversine (great circle distances) between points.

i open using other packages conversion.

i tried using library birk giving me meaningless answers lat/long greater 1000. before, trying birk, had converted lat/long representation 1 birk accepts in conv_unit function

this may off, perhaps more knowledgeable me can chime in here goes.

i found thread r-help mailing list (from 6 years ago) talks converting dms decimal degrees using formula decimal degrees = degrees + (minutes/60) + (seconds/3600).

using regular expressions find appropriate element, construct data.frame used apply above mentioned formula. multiplying coordinates south , west -1 make them negative.

xy <- c("39d47m01s n", "15d38m08s n", "12d45m01s n", "13d17m25s n",    "36d34m29s n", "24d46m34s s", "11d52m39s s", "39d47m01s n", "30d52m34s n",    "34d59m47s s")  nxy <- data.frame(degs = as.numeric(gsub("(^\\d+)(.+$)", "\\1", xy)),                   mins = as.numeric(gsub("(^.+d)(\\d+)(m.+$)", "\\2", xy)),                   secs = as.numeric(gsub("(^.+m)(\\d+)(s [a-z]$)", "\\2", xy)),                   direction = gsub("(^.+ )([a-z])$", "\\2", xy))  nxy$indec <- with(nxy, degs + (mins/60) + (secs/3600))  nxy[nxy$direction %in% c("w", "s"), "indec"] <- nxy[nxy$direction %in% c("w", "s"), "indec"] * -1  nxy     degs mins secs direction     indec 1    39   47    1         n  39.78361 2    15   38    8         n  15.63556 3    12   45    1         n  12.75028 4    13   17   25         n  13.29028 5    36   34   29         n  36.57472 6    24   46   34         s -24.77611 7    11   52   39         s -11.87750 8    39   47    1         n  39.78361 9    30   52   34         n  30.87611 10   34   59   47         s -34.99639 

or, make char2dms work you.

nxy$char2dms <- as.numeric(sp::char2dms(xy, chd = "d", chm = "m", chs = "s"))     degs mins secs direction     indec  char2dms 1    39   47    1         n  39.78361  39.78361 2    15   38    8         n  15.63556  15.63556 3    12   45    1         n  12.75028  12.75028 4    13   17   25         n  13.29028  13.29028 5    36   34   29         n  36.57472  36.57472 6    24   46   34         s -24.77611 -24.77611 7    11   52   39         s -11.87750 -11.87750 8    39   47    1         n  39.78361  39.78361 9    30   52   34         n  30.87611  30.87611 10   34   59   47         s -34.99639 -34.99639