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? Thanks in advance. Weidong Gu, Department of Medicine University of Alabama, Birmingham [[alternative HTML version deleted]]
Jorge Iván Vélez
2008-Feb-08 21:09 UTC
[R] how to extract characters from a character string
Hi Weidong, It works, but I'm completely sure could be more efficient: # x is a string DELETE=function(x){ x=as.character(x) res=NULL; for(i in 1:nchar(x)) res=c(res,substr(x,i,i)) pos=which(res=="?"|res==" ") # Detecting "?" and " " res2=res[(pos[1]+1):(pos[2]-1)] k=length(res2) res3=NULL; for(i in 1:k) res3=paste(res3,res2[i],sep="") as.numeric(RS) }> DELETE("32?35.421 N")[1] 35.421 I hope this helps. Jorge On 2/8/08, Weidong Gu <wgu@uab.edu> 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? > > > > > Thanks in advance. > > > > > > Weidong Gu, > > Department of Medicine > University of Alabama, Birmingham > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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.[[alternative HTML version deleted]]
This should do it for you:> x[1] "32?35.421 N"> sub("^.*?([[:digit:].]+) N", "\\1", x, perl=TRUE)[1] "35.421">On 2/8/08, Weidong Gu <wgu at uab.edu> 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? > > > > > Thanks in advance. > > > > > > Weidong Gu, > > Department of Medicine > University of Alabama, Birmingham > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
Gabor Grothendieck
2008-Feb-08 21:41 UTC
[R] how to extract characters from a character string
Match the start (^) followed by anything (.*) to the question mark ([?]) or (|) a space ( ) followed by anything (.*) to the end ($) and replace each of those with nothing (""). gsub("^.*[?]| .*$", "", "32?35.421 N") On Feb 8, 2008 3:36 PM, Weidong Gu <wgu at uab.edu> 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? > > > > > Thanks in advance. > > > > > > Weidong Gu, > > Department of Medicine > University of Alabama, Birmingham > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >
Peter Dalgaard
2008-Feb-08 21:43 UTC
[R] how to extract characters from a character string
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