Is there a stringr
equivalent for grep
with value
set to TRUE
? (I would like to avoid the NA
s returned by the stringr
command below.)
library(stringr)
x <- c("a", "b", "a")
grep("a", x, value = TRUE) # returns "a" "a"
str_extract(x, "a") # returns "a" NA "a"
Use str_subset
:
str_subset(x,"a")
[1] "a" "a"
The helpfile states the equivalence:
str_subset() is a wrapper around x[str_detect(x, pattern)], and is equivalent to grep(pattern, x, value = TRUE).
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