Get startedGet started for free

sub & gsub

While grep() and grepl() were used to simply check whether a regular expression could be matched with a character vector, sub() and gsub() take it one step further: you can specify a replacement argument. If inside the character vector x, the regular expression pattern is found, the matching element(s) will be replaced with replacement. sub() only replaces the first match, whereas gsub() replaces all matches.

Suppose that emails vector you've been working with is an excerpt of DataCamp's email database. Why not offer the owners of the .edu email addresses a new email address on the datacamp.edu domain? This could be quite a powerful marketing stunt: Online education is taking over traditional learning institutions! Convert your email and be a part of the new generation!

This exercise is part of the course

Intermediate R

View Course

Exercise instructions

With the advanced regular expression "@.*\\.edu$", use sub() to replace the match with "@datacamp.edu". Since there will only be one match per character string, gsub() is not necessary here. Inspect the resulting output.

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 sub() to convert the email domains to datacamp.edu
Edit and Run Code