[R] Grep command

Niels Jespersen NJN at dst.dk
Wed May 4 08:22:39 CEST 2016

> x <- c("abc","def","rst","xyz","age","age2")
> grep("^age$", x)
[1] 5
> grep("^age2$", x)
[1] 6

-----Oprindelig meddelelse-----
Fra: R-help [mailto:r-help-bounces at r-project.org] På vegne af Steven Yen
Sendt: 4. maj 2016 07:46
Til: r-help
Emne: [R] Grep command

Dear all
In the grep command below, is there a way to identify only "age" and not "age2"? In other words, I like to greb "age" and "age2"
separately, one at a time. Thanks.


[1] "abc"  "def"  "rst"  "xyz"  "age"  "age2"


[1] 6

grep("age",x) # I need to grab "age" only, not "age2"

[1] 5 6

R-help at 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.

More information about the R-help mailing list