[R] grep/regexp

Ravi Varadhan ravi.varadhan at jhu.edu
Tue Dec 1 23:47:21 CET 2015


Hi,
I would appreciate some help with using grep().  I have a bunch of variables in a data frame and I would like to select some of them using grep.  Here is an example of what I am trying to do:

vars <- c("Fr_I_total", "Fr_I_percent_of_CD4", "Ki.67_in_Fr_I_percent_of_Fr_I", "Fr_II_percent_of_CD4", "Ki.67_in_Fr_II_percent_of_Fr_II")

>From the above vector, I would like to select those variables beginning with `Fr' and containing `percent' in them.   In other words, I would like to get the variables "Fr_I_percent_of_CD4" and "Fr_II_percent_of_CD4".

How can I use grep() to do this?

More generally, are there any good online resources with examples like this for the use of grep() and regexp() in R?  I didn't find the help pages for these very user-friendly.

Thank you very much,
Ravi

Ravi Varadhan, Ph.D. (Biostatistics), Ph.D. (Environmental Engg)
Associate Professor,  Department of Oncology
Division of Biostatistics & Bionformatics
Sidney Kimmel Comprehensive Cancer Center
Johns Hopkins University
550 N. Broadway, Suite 1111-E
Baltimore, MD 21205
410-502-2619


	[[alternative HTML version deleted]]



More information about the R-help mailing list