Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep a vector and return a single TRUE or FALSE?

Tags:

regex

r

Is there a grep function in R that returns TRUE if a pattern is found anywhere in the given character vector and FALSE otherwise?

All the functions I see return a vector of the current positions of each element found.

like image 407
Paolo Avatar asked Jul 14 '12 04:07

Paolo


People also ask

What is the use of grep () Grepl () substr ()?

17.4 grepl() grepl() returns a logical vector indicating which element of a character vector contains the match. For example, suppose we want to know which states in the United States begin with word “New”. Here, we can see that grepl() returns a logical vector that can be used to subset the original state.name vector.

What does grep return r?

The grep R function returns the indices of vector elements that contain the character “a” (i.e. the second and the fourth element). The grepl function, in contrast, returns a logical vector indicating whether a match was found (i.e. TRUE) or not (i.e. FALSE).

What does the Grepl () function do?

The grepl() stands for “grep logical”. In R it is a built-in function that searches for matches of a string or string vector. The grepl() method takes a pattern and data and returns TRUE if a string contains the pattern, otherwise FALSE.

How are Regexpr Gregexpr and Regexec different than grep Grepl?

Description. grep , grepl , regexpr , gregexpr , regexec and gregexec search for matches to argument pattern within each element of a character vector: they differ in the format of and amount of detail in the results. sub and gsub perform replacement of the first and all matches respectively.


1 Answers

possibly a combination of grepl() and any()?

like

> foo = c("hello", "world", "youve", "got", "mail")
> any(grepl("world", foo))
[1] TRUE
> any(grepl("hi", foo))
[1] FALSE  
> any(grepl("hel", foo))
[1] TRUE

your questions a little unclear as to whether you want that last example to return true or not

like image 198
sayhey69 Avatar answered Oct 26 '22 04:10

sayhey69