Help-Rs,
 
I'm doing some string manipulation in a file where I converted a string date in 
mm/dd/yyyy format and returned the date yyyy.
 
I've used regexpr (hat tip to Gabor G for a very nice earlier post on this 
function) in steps (I've un-nested the code and provided it and an example of 
what I did below.  My question is: is there a more efficient way to do this.  
Specifically is there a way to use regexpr or some other string function to 
return not the first instance, but the 2nd (or for that matter 3rd, 4th or 5th 
instance) of a certain string?
 
 #first find the first occurence of "/" and create a variable for this 
firstslash <- unlist(regexpr("/", dates, fixed = TRUE)) #then use frist/ to cut 
the string field into an intermediate variable e.g., from 1/1/2008 to 1/2008. 
step1 <- substr( dates,  (firstslash + 1), nchar(dates) ) #then repeat steps 1 
and 2...there's got to be a better way step2 <- unlist(regexpr("/", step1, 
fixed = TRUE)) #then use step2 to cut string into final product e.g., from 
1/2008 to 2008. final <- substring(step1,step2 + 1, nchar(step1) )
 
Thx!
C
        [[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.

Reply via email to