Re: [R] Regular Expressions

From: Dimitris Rizopoulos <dimitris.rizopoulos_at_med.kuleuven.be>
Date: Tue, 13 May 2008 11:52:01 +0200

try this:

S <- c("World_is_beautiful", "one_two_three_four","My_book")

sapply(strsplit(S, "_"), tail, n = 2)[1, ] # or
sapply(strsplit(S, "_"), function(x) x[length(x) - 1])

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


Hi R,

Again struck with regular expressions...

Suppose,

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....

Shubha Karanth | Amba Research

Ph +91 80 3980 8031 | Mob +91 94 4886 4510

Bangalore * Colombo * London * New York * San Josť * Singapore * www.ambaresearch.com

This e-mail may contain confidential and/or privileged i...{{dropped:13}}


> ______________________________________________
> 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.
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



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. Received on Tue 13 May 2008 - 09:55:49 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 13 May 2008 - 10:30:38 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive