[R] Regular Expressions

Richard.Cotton at hsl.gov.uk Richard.Cotton at hsl.gov.uk
Tue May 13 12:02:24 CEST 2008


> S=c("World_is_beautiful", "one_two_three_four","My_book")

> I need to extract the last but one element of the strings. So, my 
> output should look like:
 
> Ans=c("is","three","My")

> gsub() can do this...but wondering how do I give the regular 
expression....

sapply(strsplit(S, "_"), function(x) x[length(x)-1])

You could use regular expressions, but I think it would only be 
complicating things.

Regards,
Richie.

Mathematical Sciences Unit
HSL


------------------------------------------------------------------------
ATTENTION:

This message contains privileged and confidential inform...{{dropped:20}}



More information about the R-help mailing list