R check if string is in column name code example
Example 1: check if a string is contained in a column R
str_detect("aecfg", letters)
grepl(letters,"aecfg")
# [1] TRUE
# Warning message:
# In grepl(letters, "aecfg") :
# argument 'pattern' has length > 1 and only the first element will be used
identical(str_detect("aecfg", letters),
Vectorize(grepl,"pattern", USE.NAMES=FALSE)(letters,"aecfg")) # [1] TRUE
Example 2: check if a string is contained in a column R
fruit <- c("apple", "banana", "pear", "pinapple") # [1] TRUE
identical(str_detect(fruit, "a"), grepl("a",fruit)) # [1] TRUE
identical(str_detect(fruit, "^a"), grepl("^a",fruit)) # [1] TRUE
identical(str_detect(fruit, "a$"), grepl("a$",fruit)) # [1] TRUE
identical(str_detect(fruit, "b"), grepl("b",fruit)) # [1] TRUE
identical(str_detect(fruit, "[aeiou]"), grepl("[aeiou]",fruit)) # [1] TRUE