Test if characters are in a string

mike picture mike · Apr 12, 2012 · Viewed 500.2k times · Source

I'm trying to determine if a string is a subset of another string. For example:

chars <- "test"
value <- "es"

I want to return TRUE if "value" appears as part of the string "chars". In the following scenario, I would want to return false:

chars <- "test"
value <- "et"

Answer

smu picture smu · Apr 12, 2012

Use the grepl function

grepl( needle, haystack, fixed = TRUE)

like so:

grepl(value, chars, fixed = TRUE)
# TRUE

Use ?grepl to find out more.