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.
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.
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).
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With