[R] grep

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sat May 8 19:20:23 CEST 2021

Regular expression patterns are not vectorized... only the data to be searched are. Use one of the many websites dedicated to tutoring regular expressions to learn how they work. (Using function names like "names" as data names is bad practice.)

nms <- c( "x1.one", "x1.black", "x1.othrrace", "x1.moddkna", "x1.conserv", "x1.nstrprty", "x1.strrep", "x1.sevngprt", "x2.one", "x2.black", "x2.othrrace", "x2.moddkna", "x2.conserv", "x2.nstrprty", "x2.strrep", "x2.sevngprt" )

grep( "black|conserv", nms, value = TRUE )

On May 8, 2021 10:00:12 AM PDT, Steven Yen <styen using ntu.edu.tw> wrote:
>Below, the first command simply creates a list of 16 names (labels) 
>which can be ignore.
>In the 2nd and 3rd commands, I am able to identify names containing
>In line 4, I am trying to identify names containing "black" or
>but obviously it does not work. Can someone help? Thanks.
> > names<-names(tp.nohs$estimate)[c(1:8,58:65)]; names
>  [1] "x1.one"      "x1.black"    "x1.othrrace" "x1.moddkna" 
>"x1.conserv"  "x1.nstrprty"
> [7] "x1.strrep"   "x1.sevngprt" "x2.one"      "x2.black" "x2.othrrace"
>[13] "x2.conserv"  "x2.nstrprty" "x2.strrep"   "x2.sevngprt"
> > grep("black",names,value=TRUE)
>[1] "x1.black" "x2.black"
> > grep("black",names,value=FALSE)
>[1]  2 10
> > grep(c("black","conserv"),names,value=TRUE)
>[1] "x1.black" "x2.black"
>Warning message:
>In grep(c("black", "conserv"), names, value = TRUE) :
>  argument 'pattern' has length > 1 and only the first element will be
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

Sent from my phone. Please excuse my brevity.

More information about the R-help mailing list