[R] matching a string with multiple conditions using grep

arun smartpink111 at yahoo.com
Sun Jun 24 01:13:50 CEST 2012


Hi,

Try this:
vec1<-c("A_cont_1", "A_cont_12", "B_treat_8", "AB_cont_22", "cont_21_Aa")
vec2<-grep("(A){0,1}.*cont.*2",vec1)

vec1[vec2]
[1] "A_cont_12"  "AB_cont_22" "cont_21_Aa"


A.K.




----- Original Message -----
From: Zhipeng Wang <wangz at kuhp.kyoto-u.ac.jp>
To: r-help at r-project.org
Cc: 
Sent: Saturday, June 23, 2012 6:19 AM
Subject: [R] matching a string with multiple conditions using grep

Suppose I have a vector  ["A_cont_1", "A_cont_12", "B_treat_8",
"AB_cont_22", "cont_21_Aa"], I hope I can extract the strings which include
3 short strings, say "A", "cont" and "2",  that is to say, "A_cont_12",
"AB_cont_22" and  "cont_21_Aa" will be extract, using a relatively short
code (using grep?).
Would you please to give some idea? Thank you !

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




More information about the R-help mailing list