Re: [R] Re gular Expression help

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Fri, 07 Nov 2008 19:15:01 -0500

Here are a few more solutions. x is the input vector of character strings.

The first is a slightly shorter version of one of Wacek's. The next three all create an anonymous grouping variable (using sub, substr/gsub and strapply respectively) whose components are "p" and "q" and then tapply is used to separate out the corresponding components of x according to the grouping:

sapply(c(p = "^[^pq]*p", q = "^[^pq]*q"), grep, x = x, value = TRUE)

tapply(x, sub("^[^pq]*(.).*", "\\1", x), c)

tapply(x, substr(gsub("[^pq]", "", x), 1, 1), c)

library(gsubfn)
tapply(x, strapply(x, "^[^pq]*(.)", simplify = c), c)

On Fri, Nov 7, 2008 at 1:09 PM, Rajasekaramya <ramya.victory_at_gmail.com> wrote:
>
> hi there
>
> I have a vector with a set of data.I just wanna seperate them based on the
> first p and q values metioned within the data.
>
> [1] chr10p15.3 /// chr3q29 /// chr4q35 /// chr9q34.3
> [2] chr1q22-q24
> [3] chr1q22-q24
> [4] chr1pter-q24
> [5] chr1pter-q24
> [6] chr1pter-q24
>
> i used a regular expression [+q*] to match up the values but it matches q
> found anywhere i know i have written like that but i jus want it to match
> the first p or q values.
>
> my result should be for q and
> [2] chr1q22-q24
> [3] chr1q22-q24
>
> for p
> [1] chr10p15.3 /// chr3q29 /// chr4q35 /// chr9q34.3
> [4] chr1pter-q24
> [5] chr1pter-q24
> [6] chr1pter-q24
>
>
>
> --
> View this message in context: http://www.nabble.com/Regular-Expression-help-tp20385971p20385971.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



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 Sat 08 Nov 2008 - 00:18:29 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 Sat 08 Nov 2008 - 22:30:24 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