This practice aims to make you more familiar with the string format and operations we often need to match or subset strings while preparing data for analysis. Try to solve these string operations tasks. This exercise does not count toward your grade. It is just for practice!
String Basics: Exercises
-
In code that doesn't use stringr, you'll often see
paste()
andpaste0()
. What's the difference between the two functions? What stringr function are they equivalent to? How do the functions differ in their handling ofNA
? -
In your own words, describe the difference between the
sep
andcollapse
arguments tostr_c()
. -
Use
str_length()
andstr_sub()
to extract the middle character from a string. What will you do if the string has an even number of characters? -
What does
str_wrap()
do? When might you want to use it? -
What does
str_trim()
do? What's the opposite ofstr_trim()
? -
Write a function that turns (e.g.) a vector
c("a", "b", "c")
into the stringa, b, and c
. Think carefully about what it should do if given a vector of length 0, 1, or 2.
Matching patterns with regular expressions
Basic matches: Exercises
-
Explain why each of these strings don't match a
\
:"\"
,"\\"
,"\\\"
. -
How would you match the sequence
"'\
? -
What patterns will the regular expression
\..\..\..
match? How would you represent it as a string?
Character classes and alternatives: Exercises
-
Create regular expressions to find all words that:
-
Start with a vowel.
-
That only contain consonants. (Hint: thinking about matching "not"-vowels.)
-
End with
ed
, but not witheed
. -
End with
ing
orise
.
-
-
Empirically verify the rule "i before e except after c".
-
Is "q" always followed by a "u"?
-
Write a regular expression that matches a word if it's probably written in British English, not American English.
-
Create a regular expression that will match telephone numbers as commonly written in your country.
Source: H. Wickham and G. Grolemund, https://r4ds.had.co.nz/strings.html This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.