[R] Grep command

Omar André Gonzáles Díaz oma.gonzales at gmail.com
Wed May 4 08:15:55 CEST 2016


Hi Steven,

grep uses regex... so you can use this:

-grep("age$",x): it says: match "a", then "g", then "e" and stop.  The "$"
menas until here and no more.

> grep("age$",x)
[1] 5

2016-05-04 1:02 GMT-05:00 Jim Lemon <drjimlemon at gmail.com>:

> Hi Steven,
> If this is just a one-off, you could do this:
>
> grepl("age",x) & nchar(x)<4
>
> returning a logical vector containing TRUE for "age" but not "age2"
>
> Jim
>
>
> On Wed, May 4, 2016 at 3:45 PM, Steven Yen <syen04 at gmail.com> wrote:
> > 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.
> >
> > x<-c("abc","def","rst","xyz","age","age2")
> > x
> >
> > [1] "abc"  "def"  "rst"  "xyz"  "age"  "age2"
> >
> > grep("age2",x)
> >
> > [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.
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list