check if a string is contained in a column R code example
Example 1: check if a string is contained in a column R
str_detect("aecfg", letters)
grepl(letters,"aecfg")
identical(str_detect("aecfg", letters),
Vectorize(grepl,"pattern", USE.NAMES=FALSE)(letters,"aecfg"))
Example 2: check if a string is contained in a column R
fruit <- c("apple", "banana", "pear", "pinapple")
identical(str_detect(fruit, "a"), grepl("a",fruit))
identical(str_detect(fruit, "^a"), grepl("^a",fruit))
identical(str_detect(fruit, "a$"), grepl("a$",fruit))
identical(str_detect(fruit, "b"), grepl("b",fruit))
identical(str_detect(fruit, "[aeiou]"), grepl("[aeiou]",fruit))