Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stringr equivalent to grep

Tags:

r

stringr

Is there an stringr equivalent to base R's grep function?

I want to have the index of the string that matches. Example:

grep("F|Y", LETTERS)
[1]  6 25

With stringr my workaround would be using which as follows:

which(str_detect(LETTERS, "F|Y"))
[1]  6 25
like image 983
Rentrop Avatar asked Jun 23 '16 15:06

Rentrop


1 Answers

Sorry for the late answer but it might be helpful for future visitors:

Now you can use str_which(string, pattern) which is a wrapper around which(str_detect(string, pattern)) and equivalent to grep(pattern, string).

str_which(LETTERS, "F|Y")
[1]  6 25

More details at: http://stringr.tidyverse.org/reference/str_subset.html

like image 158
allanvc Avatar answered Sep 29 '22 14:09

allanvc