Summer Deals - hours only!Up to 80% off on all courses and bundles.-Close
Strings basics
2. Escape sequences
Regular expressions
Modifying strings
Summary

Instruction

Great! What if we want to include different quotes in a string? Take a look:

string <- "I read Hadley Wickham's book titled \"R for Data Science\"."

Since we used double quotes (") as delimiters of the string, we needed to escape them, when we wanted to write about the title of a book (\"). The escape sequence starts with a backslash \ followed by the character we're escaping (in the example above – a double quote ").

The most popular escape sequences are:

\" For escaping the double quote characters when they are used as string delimiters.
\' For escaping the single quote characters when they are used as string delimiters.
\n For escaping the newline character.
\t For escaping the tab character.
\\ When we want to use the backslash character. This will be extremely necessary later in the course.

Let's use some of these characters in an example.

string1 <- "\tI read Hadley Wickham and Garrett Grolemund's book titled \"R for Data Science\",\nwhich was published in 2017."

Also, instead of using escape sequences for tabs and newlines, you may type them:

string2 <- "  I read Hadley Wickham and Garrett Grolemund's book titled \"R for Data Science\",
which was published in 2017."

Those strings (string1 and string2) are identical.

Exercise

Run the example to see that two different ways of writing a string give the same result.