Practice: Strings

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

  1. In code that doesn't use stringr, you'll often see paste() and paste0(). What's the difference between the two functions? What stringr function are they equivalent to? How do the functions differ in their handling of NA?

  2. In your own words, describe the difference between the sep and collapse arguments to str_c().

  3. Use str_length() and str_sub() to extract the middle character from a string. What will you do if the string has an even number of characters?

  4. What does str_wrap() do? When might you want to use it?

  5. What does str_trim() do? What's the opposite of str_trim()?

  6. Write a function that turns (e.g.) a vector c("a", "b", "c") into the string a, 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

  1. Explain why each of these strings don't match a \: "\", "\\", "\\\".

  2. How would you match the sequence "'\?

  3. What patterns will the regular expression \..\..\.. match? How would you represent it as a string?


Character classes and alternatives: Exercises

  1. Create regular expressions to find all words that:

    1. Start with a vowel.

    2. That only contain consonants. (Hint: thinking about matching "not"-vowels.)

    3. End with ed, but not with eed.

    4. End with ing or ise.

  2. Empirically verify the rule "i before e except after c".

  3. Is "q" always followed by a "u"?

  4. Write a regular expression that matches a word if it's probably written in British English, not American English.

  5. 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
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 3.0 License.

Last modified: Thursday, December 15, 2022, 4:43 PM