[R] regexp help needed

Kornelius Rohmeyer kornelius.rohmeyer at small-projects.de
Fri Nov 28 10:26:45 CET 2008


Lauri Nikkinen:
> Hello,
> 
> I have a vector of dates and I would like to grep the year component
> from this vector (= all digits
> after the last punctuation character)
> 
> dates <- c("28.7.08","28.7.2008","28/7/08", "28/7/2008", "28/07/2008",
> "28-07-2008", "28-07-08")
> 
> the resulting vector should look like
> 
> "08" "2008" "08" "2008" "2008" "2008" "08"

unlist(lapply(strsplit(dates,c("\\.|/|-")),function(x){x[length(x)]}))
[1] "08"   "2008" "08"   "2008" "2008" "2008" "08"  

Best regards, Kornelius.

> 
> I tried something like (Perl style) with no success
> 
> grep("[[:punct:]]?\\d", dates, value=T, perl=T)
> 
> Any ideas?
> 
> -Lauri
> 
> ______________________________________________
> 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.



More information about the R-help mailing list