[R] Grep command

Joyce Robbins joycerobbins1 at gmail.com
Fri May 20 02:03:21 CEST 2016


I'm not sure you need grep:

> all %in% some
[1]  TRUE FALSE  TRUE FALSE FALSE  TRUE

On Thu, May 19, 2016 at 7:58 PM, MacQueen, Don <macqueen1 at llnl.gov> wrote:

> Start with:
>
> > all <- c("ants","birds","cats","dogs","elks","fox")
> > all[grep('ants|cats|fox',all)]
> [1] "ants" "cats" "fox"
>
> Then construct the first arg to grep:
>
> > some <- c("ants","cats","fox")
> > all[ grep( paste(some,collapse='|') , all)]
> [1] "ants" "cats" "fox"
>
>
>
> --
> Don MacQueen
>
> Lawrence Livermore National Laboratory
> 7000 East Ave., L-627
> Livermore, CA 94550
> 925-423-1062
>
>
>
>
>
> On 5/19/16, 4:09 PM, "R-help on behalf of Steven Yen"
> <r-help-bounces at r-project.org on behalf of syen04 at gmail.com> wrote:
>
> >What is a good way to grep multiple strings (say in a vector)? In the
> >following, I grep ants, cats, and fox separately and concatenate them,
> >is there a way to grep the trio in one action? Thanks.
> >
> >all<-c("ants","birds","cats","dogs","elks","fox"); all
> >[1] "ants"  "birds" "cats"  "dogs"  "elks"  "fox"
> >some<-c("ants","cats","fox"); some
> >[1] "ants" "cats" "fox"
> >j<-c(
> >   grep(some[1],all,value=F),
> >   grep(some[2],all,value=F),
> >   grep(some[3],all,value=F)); j; all[j]
> >[1] 1 3 6
> >[1] "ants" "cats" "fox"
> >
> >
> >       [[alternative HTML version deleted]]
> >
> >______________________________________________
> >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