Re: [R] matching a string with multiple conditions using grep

2012-06-23 Thread arun
Original Message - From: Zhipeng Wang To: r-help@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&qu

Re: [R] matching a string with multiple conditions using grep

2012-06-23 Thread Zhipeng Wang
ot;, "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

Re: [R] matching a string with multiple conditions using grep

2012-06-23 Thread Rui Barradas
Hello, Try the following. wanted <- c("A_cont_12", "AB_cont_22", "cont_21_Aa") x <- c("A_cont_1", "A_cont_12", "B_treat_8", "AB_cont_22", "cont_21_Aa") pattern <- c("A", "cont", "2") ix <- Reduce(`&`, lapply(pattern, grepl, x)) # This does the trick identical(wanted, x[ix]) See ?Reduce.

[R] matching a string with multiple conditions using grep

2012-06-23 Thread Zhipeng Wang
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 gr