[R] degree-min-sec data

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jan 9 23:41:51 CET 2004


George_Heine at blm.gov writes:

> Hello -
> 
> Have both astronomic and geodetic data sets with values in the form
> "ddd:mm:ss.sssss", where dd is an integer between -180 and 180, mm is an
> integer between 0 and 60, and ss is a floating-point
> number between 0 and 60.0.  In order to do anything useful with these
> values they need to be turned into their "decimal degree" equivalent.
> 
> Assuming the data is a vector y, the following works, sort of:
> 
> z<-strsplit(as.character(y),split=":")
> zz<-sapply(z,as.numeric)
> x<- t(c(1,1/60,1/3600) %*% zz)
> 
> Trouble comes if there are any NA's in the data.  In this case, R refuses
> to coerce zz into matrix form, but leaves it as a "list of lists".  How can
> the above routine be patched up to pass NAs through?
> I can think of some inelegant solutions (e.g.  recoding NA as "-200:-1:-1")
> but surely there is a better way!
> Or is this the wrong technique?

Not necessarily. I might try something like

z <- strsplit(ifelse(is.na(y),y,"::NA"))
..etc..


There are also fancier possibilities:

z <- scan(textConnection(y),sep=":",fill=TRUE,what=list(a=0,b=0,c=0))
x <- z$a + z$b/60 + z$c/3600

or even

x <- with(z, a + b/60 + c/3600)

> More generally, is there a way to "unzip" a character vector along some
> split marker, handling irregulatirites gracefully?  For example, from
> 
> y<-c("aaa#bbb", "ccc#ddd", "eee","fff"),
> 
> strsplit(y,split="#") would produce
> 
> c(c("aaa","bbb"), c("ccc","ddd"), c("eee","fff")).
> 
> Is there a way to produce instead
> 
> c(c("aaa","ccc","eee"),c("bbb","ddd","fff")) ?

?? Was that what you intended to write? 

> dput(strsplit(y,split="#"))
list(c("aaa", "bbb"), c("ccc", "ddd"), "eee", "fff")


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907




More information about the R-help mailing list