Zhipeng Wang
2012-Jun-23 10:19 UTC
[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]]
Rui Barradas
2012-Jun-23 10:52 UTC
[R] matching a string with multiple conditions using grep
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. Hope this helps, Rui Barradas Em 23-06-2012 11:19, Zhipeng Wang escreveu:> 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.
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.
Apparently Analagous Threads
- Connect observed values by a smooth curve, is there any method to get coordinate value of arbitrary point on the curve?
- Error in get(Info[i, 1], envir = env) : internal error -3 in R_decompress1
- kernel oops when sync after rm a file at full disk
- [ANNOUNCE] intel-gpu-tools 1.10
- Wine release 5.0.2