> On Dec 1, 2015, at 2:47 PM, Ravi Varadhan wrote:
>
> 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?
> grep("^Fr.*percent", vars, value=TRUE)
[1] "Fr_I_percent_of_CD4" “Fr_II_percent_of_CD4"
> 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.
>
There are several interactive regex websites where you can get commented and
tested solutions. They have the disadvantage that they don’t use the extra
backslashes that R requires since both R and regex use backslashes as escape
characters.
I learned regex from the R's ?regex page and by watching Gabor Grothendeick’s
postings to Rhelp. (I’ve probably gone over the ?regex page 20 or thirty times.)
These days there are a great many regex questions and answers on Stack Overflow
, many of them again written by Maestro Grothendeick.
—
David Winsemius
Alameda, CA, USA
__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.