[R] how to extract characters from a character string

2008-02-08 Thread Weidong Gu
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

Re: [R] how to extract characters from a character string

2008-02-08 Thread Jorge Iván Vélez
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

Re: [R] how to extract characters from a character string

2008-02-08 Thread Peter Dalgaard
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

Re: [R] how to extract characters from a character string

2008-02-08 Thread Gabor Grothendieck
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 [EMAIL PROTECTED] wrote: Hi, I ran into a problem

Re: [R] how to extract characters from a character string

2008-02-08 Thread jim holtman
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 [EMAIL PROTECTED] 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