Convert longitude/latitude formats from DMS to D.dec or DM.dec

Test of different packages to convert long/lat data

Package sp

Several packages can do the job. First, let use sp package. The orignal data can be in character string or in numbers.

> library("sp")
> essai <- char2dms(from="01d33'01.6\"N", chd="d", chm="'")
> as.numeric(essai)
[1] 1.550444
> essai <- char2dms(from="01°33'01.6\"N", chd="°", chm="'")

Take care, the " character cannot be omitted:

> essai <- char2dms(from="01°33'01.6N", chd="°", chm="'")
> essai
[1] 1d33'N
> as.numeric(essai)
[1] 1.55

Numeric data can be used directly using:

> essai <- new(Class = "DMS", WS=FALSE, deg=1, min=33, sec=1.6, NS=TRUE)
> essai
[1] 1d33'1.6"N
> as.numeric(essai)
[1] 1.550444

And such DMS class object can be obtained from D.dec format using:

> dd2dms(1.550444, NS=TRUE)
[1] 1d33'1.598"N

essai is a S4 object and elements can be accessed using @

> str(essai)
Formal class 'DMS' [package "sp"] with 5 slots
  ..@ WS : logi FALSE
  ..@ deg: int 1
  ..@ min: int 33
  ..@ sec: num 1.6
  ..@ NS : logi TRUE
> essai@deg
[1] 1

________________
Package epiR

Let try now with the epic library. The format data must be in matrix with one, two or 3 columns.

> library("epiR")
Le chargement a nécessité le package : survival
Package epiR 0.9-77 is loaded
Type help(epi.about) for summary information


> dat <- matrix(c(1, 33, 1.6), nrow=1)
> epi.dms(dat)
         [,1]
[1,] 1.550444
> epi.dms(dat)[1, 1]
[1] 1.550444
> epi.dms(matrix(c(1, 33.02665), nrow=1))[1, 1]
[1] 1.550444

en conclusion, sp library is more flexible but epiR is the only one to enter data using format DM.dec.

Commentaires

Posts les plus consultés de ce blog

Standard error from Hessian Matrix... what can be done when problem occurs

stepAIC from package MASS with AICc

Install treemix in ubuntu 20.04