by Sai gowtham

How to Check if string contains certain characters in R

To check if a string contains certain characters or not, we can use the grepl() function in R language.

Here is an example that checks the ll characters in the Hello string.

str <- "Hello"
chars <- "ll"


grepl(chars, str, fixed = TRUE)

Output:

> grepl(chars, str, fixed = TRUE)

[1] TRUE

False case

str <- "Hello"
chars <- "es"


grepl(chars, str, fixed = TRUE)

Output:

> grepl(chars, str, fixed = TRUE)

[1] FALSE

Top Udemy Courses

JavaScript - The Complete Guide 2020 (Beginner + Advanced)
JavaScript - The Complete Guide 2020 (Beginner + Advanced)
45,614 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)
130,921 students enrolled
21 hours of video content
View Course