To check if a string contains a given substring in R, you can use the 'grepl' function.
In this example,
containsSubstring <- function(str, substr) {
return(grepl(substr, str))
}
str <- 'Hello World'
substr <- 'World'
contains <- containsSubstring(str, substr)
print(paste('String contains substring', substr, ':', contains))
[1] "String contains substring World : TRUE"
In this tutorial, we learned How to Check if a String contains given Substring in R language with well detailed examples.