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

2012-06-23 Thread arun
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 To: r-help@r-project.org Cc: Sent: Saturday, June 23, 201

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

2012-06-23 Thread Zhipeng Wang
Dear Barradas, and Dear A.K. Thank you so much for the solutions. I got them. I think I will study more about regular expression and apply family. Best Regards, Wang 2012/6/24 arun > Hi, > > Try this: > vec1<-c("A_cont_1", "A_cont_12", "B_treat_8", "AB_cont_22", "cont_21_Aa") > vec2<-grep("(A

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.