Get Started

grepl & grep

In their most basic form, regular expressions can be used to see whether a pattern exists inside a character string or a vector of character strings. For this purpose, you can use:

  • grepl(), which returns TRUE when a pattern is found in the corresponding character string.
  • grep(), which returns a vector of indices of the character strings that contains the pattern.

Both functions need a pattern and an x argument, where pattern is the regular expression you want to match for, and the x argument is the character vector from which matches should be sought.

In this and the following exercises, you'll be querying and manipulating a character vector of email addresses! The vector emails has been pre-defined so you can begin with the instructions straight away!

This is a part of the course

“Intermediate R”

View Course

Exercise instructions

  • Use grepl() to generate a vector of logicals that indicates whether these email addresses contain "edu". Print the result to the output.
  • Do the same thing with grep(), but this time save the resulting indexes in a variable hits.
  • Use the variable hits to select from the emails vector only the emails that contain "edu".

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# The emails vector has already been defined for you
emails <- c("[email protected]", "[email protected]", "[email protected]",
            "invalid.edu", "[email protected]", "[email protected]")

# Use grepl() to match for "edu"


# Use grep() to match for "edu", save result to hits


# Subset emails using hits

This exercise is part of the course

Intermediate R

BeginnerSkill Level
4.5+
131 reviews

Continue your journey to becoming an R ninja by learning about conditional statements, loops, and vector functions.

Mastering R programming is not only about understanding its programming concepts. Having a solid understanding of a wide range of R functions is also important. This chapter introduces you to many useful functions for data structure manipulation, regular expressions, and working with times and dates.

Exercise 1: Useful FunctionsExercise 2: Mathematical utilitiesExercise 3: Find the errorExercise 4: Data UtilitiesExercise 5: Find the error (2)Exercise 6: Beat Gauss using RExercise 7: Regular ExpressionsExercise 8: grepl & grep
Exercise 9: grepl & grep (2)Exercise 10: sub & gsubExercise 11: sub & gsub (2)Exercise 12: Times & DatesExercise 13: Right here, right nowExercise 14: Create and format datesExercise 15: Create and format timesExercise 16: Calculations with DatesExercise 17: Calculations with TimesExercise 18: Time is of the essence

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free