check if column contains string r code example

Example 1: check if column contains string in vector r

v <- c('a','b','c','e')

'b' %in% v
## returns TRUE

match('b',v)
## returns the first location of 'b', in this case: 2

Example 2: 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

Tags:

Misc Example