[R] how to extract characters from a character string
Peter Dalgaard
p.dalgaard at biostat.ku.dk
Fri Feb 8 22:43:55 CET 2008
Weidong Gu wrote:
> Hi, I ran into a problem when I complied a dataset with UTM coordinates.
> For calculating distances between sites, I need to reformat the
> coordinates from, for example,
>
>
>
> 32?35.421 N, to 35.421, i.e. I need to delete all digits before symbol ?
> and a space and N at the end of the string. What functions I should use?
>
>
>
One of the regexpr family. Beware that this stuff can be quite
maddening, but here's one option:
> x <- "32?35.421 N"
> sub("^.*\\?([.0-9]*) N$", "\\1", x)
[1] "35.421"
or less general but more straightforward:
> sub(" N$", "", sub("^.*\\?", "", x))
[1] "35.421"
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
O__ ---- Peter Dalgaard Øster Farimagsgade 5, Entr.B
c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K
(*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
More information about the R-help
mailing list