[R] String manipulation with regexpr, got to be a better way

Eik Vettorazzi E.Vettorazzi at uke.de
Fri Sep 30 10:45:06 CEST 2011


Hi Chris,
why not using routines for dates
dates <- c("09/10/2003", "10/22/2005")
format(strptime(dates,format="%m/%d/%Y"),"%Y")

or take just the last 4 chars from dates
gsub(".*([0-9]{4})$","\\1",dates)

cheers

Am 29.09.2011 16:23, schrieb Chris Conner:
> 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 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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790

--
Pflichtangaben gemäß Gesetz über elektronische Handelsregister und Genossenschaftsregister sowie das Unternehmensregister (EHUG):

Universitätsklinikum Hamburg-Eppendorf; Körperschaft des öffentlichen Rechts; Gerichtsstand: Hamburg

Vorstandsmitglieder: Prof. Dr. Jörg F. Debatin (Vorsitzender), Dr. Alexander Kirstein, Joachim Prölß, Prof. Dr. Dr. Uwe Koch-Gromus 



More information about the R-help mailing list