9.1 Introduction

Topics and terms covered in this chapter.

Terminology:

  • Text objects are called strings or text strings. We will use the terms text and strings interchangeably.

  • Text contains characters (i.e., letters, numbers, and other symbols). In R, its data type and mode are also called character.

  • Larger units of text are often called documents. Loading, reading or searching through text strings is also referred to as parsing.

9.1.1 Objectives

After working through this chapter, you should be able to:

  1. understand that text consists of strings,
  2. combine and dissect strings of text,
  3. read and write regular expressions,
  4. use stringr commands to find and replace text strings.

9.1.2 Data

Data used in this chapter.

9.1.3 Preparation

Preliminaries

This chapter assumes that you have read and worked through Chapter 14: Strings of the r4ds textbook (Wickham & Grolemund, 2017). Based on this background, we examine essential commands of base R and the stringr package (Wickham, 2019) in the context of examples and exercises.

Getting ready

  • Create an R Markdown (.Rmd) document (for instructions, see Appendix E and the templates linked in Section E.2).

  • Structure your document by inserting headings and empty lines between different parts. Here’s an example how your initial file could look:

---
title: "Chapter 09: Text data"
author: "Your name"
date: "2020 February 10"
output: html_document
---

Add text or code chunks here.

# Exercises (09: Text data)
  
## Exercise 1
  
## Exercise 2
  
etc. 
  
<!-- The end (eof). -->
  • Create an initial code chunk below the header of your .Rmd file that loads the R packages of the tidyverse (and see Section E.3.3 if you want to get rid of the messages and warnings of this chunk in your HTML output).

  • Save your file (e.g., as nr_name.Rmd in the R folder of your current project) and remember saving and knitting it regularly as you keep adding content to it.

References

Wickham, H. (2019). stringr: Simple, consistent wrappers for common string operations. Retrieved from https://CRAN.R-project.org/package=stringr

Wickham, H., & Grolemund, G. (2017). R for data science: Import, tidy, transform, visualize, and model data. Retrieved from http://r4ds.had.co.nz