Quotes
Let's get started by entering some strings in R. In the video you saw that you use quotes to tell R to interpret something as a string. Both double quotes ("
) and single ('
) quotes work, but there are some guidelines for which to use.
First, you should prefer double quotes ("
) to single quotes ('
). That means, whenever you are defining a string your first intuition should be to use "
.
Unfortunately if your string has "
inside it, R will interpret the double quote as "this is the end of the string", not as "this is the character "
". This is one time you can forget the first guideline and use the single quote, '
, to define the string.
Finally, there are cases where you need both '
and "
inside the string. In this case, fall back to the first guideline and use "
to define the string, but you'll have to escape any double quotes inside the string using a backslash (i.e. \"
).
To practice, you are going to enter a few lines from Lewis Carroll's Alice's Adventures in Wonderland. Alice has just arrived at the tea party…
This is a part of the course
“String Manipulation with stringr in R”
Exercise instructions
Following the guidelines for using quotes, define the three strings, line1
, line2
and line3
:
- Line 1:
The table was a large one, but the three were all crowded together at one corner of it:
- Line 2:
"No room! No room!" they cried out when they saw Alice coming.
- Line 3:
"There's plenty of room!" said Alice indignantly, and she sat down in a large arm-chair at one end of the table.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Define line1
line1 <- ___
# Define line2
line2 <- ___
# Define line3
line3 <- ___
This exercise is part of the course
String Manipulation with stringr in R
Learn how to pull character strings apart, put them back together and use the stringr package.
You'll start with some basics: how to enter strings in R, how to control how numbers are transformed to strings, and finally how to combine strings together to produce output that combines text and nicely formatted numbers.
Exercise 1: Welcome!Exercise 2: QuotesExercise 3: What you see isn't always what you haveExercise 4: Escape sequencesExercise 5: Turning numbers into stringsExercise 6: Using format() with numbersExercise 7: Controlling other aspects of the stringExercise 8: formatC()Exercise 9: Putting strings togetherExercise 10: Annotation of numbersExercise 11: A very simple tableExercise 12: Let's order pizza!What is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.