[R] grep

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Sun May 9 08:41:17 CEST 2021


Hello,

Maybe instead of a loop, vectorize with logical indices.


i1 <- is.na(jindex)
i2 <- is.numeric(jindex)
if(any(!i1)){
   if(any(!i2)){
     words <- jindex[!i1 & !i2]
     pattern <- paste(words, collapse = "|")
     jindex <- grep(pattern = pattern, x.label, value = FALSE)
   }
   jj <- jindex[!i1]
   x.label <- x.label[jj]
}


Or even simpler

if(any(!i1 & !i2)){
   words <- jindex[!i1 & !i2]
   pattern <- paste(words, collapse = "|")
   jindex <- grep(pattern = pattern, x.label, value = FALSE)
   jj <- jindex[!i1]
   x.label <- x.label[jj]
}


Hope this helps,

Rui Barradas

Às 02:54 de 09/05/21, Steven Yen escreveu:
> Thank to Rui, Jeff, and Bert. They are all very useful.
> Somewhat related is the following, in which jindex is a numeric or 
> alphanumeric vector in a function that starts with
> 
> try<-function(...., jindex=NA)
> 
> In the if loop, in the first line I am trying to determine whether the 
> vector jindex is NA;
> In the second line, I am trying to determine whether elements in vector 
> jindex is are all non-numeric.
> 
> Not sure how so I tried to judge by the first element of jindex. Any 
> better way? Thannks.
> 
>    if (!is.na(jindex[1])){       # like to improve this line
>      if(!is.numeric(jindex)[1]){ # like to improve this line
>        words  <-jindex
>        pattern<-paste(words,collapse="|")
>        jindex <-grep(pattern=pattern,x.label,value=FALSE)
>      }
>      jj<-jindex; x.label<-x.label[jj]
>    }
> 
> On 2021/5/9 上午 03:02, Rui Barradas wrote:
>> Hello,
>>
>> The pattern can be assembled with paste(., collapse = "|").
>> With the same vector of names, nms:
>>
>>
>> words <- c("black","conserv")
>> pattern <- paste(words, collapse = "|")
>> grep(pattern = pattern, nms, value = TRUE)
>> #[1] "x1.black"   "x1.conserv" "x2.black"   "x2.conserv"
>>
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Às 18:20 de 08/05/21, Jeff Newmiller escreveu:
>>> 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
>>>> "black".
>>>>
>>>> In line 4, I am trying to identify names containing "black" or
>>>> "conserv"
>>>> 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"
>>>>
>>>> "x2.moddkna"
>>>> [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
>>>> used
>>>>
>>>> ______________________________________________
>>>> R-help using 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