6 Selecting data values

 

This chapter covers

  • Manipulating strings
  • Selecting groups of values from different structures
  • Conditionally selecting values
  • Replacing certain values
  • Manipulating data.frames in a better way: dplyr

Now that you’ve learned how to group values together into larger structures, it’s going to be useful to access smaller components of those from time to time, be it a column from a data.frame, a row from a matrix, or part of a string. Extracting data from larger structures is both one of the most common and most dangerous (because of various R quirks) operations that you’ll perform with the language. Knowing what the dangers are and to expect them rather than be surprised by them is a defensive strategy, and there’s value in understanding what’s going on. Of course, R being an extensible language means that it only takes a bit more effort to replace the “bad” bits with something that makes more sense — and sure enough, that’s where we’re leading.

6.1 Text processing

6.1.1 Text matching

6.1.2 Substrings

6.1.3 Text substitutions

6.1.4 Regular expressions

6.2 Selecting components from structures

6.2.1 Vectors

6.2.2 Lists

6.2.3 Matrices

6.3 Replacing values

6.4 data.frames and dplyr

6.4.1 dplyr verbs

6.4.2 Non-standard evaluation

6.4.3 Pipes

6.4.4 Subsetting data.frame the hard way

6.5 Replacing NA

6.6 Selecting conditionally

6.7 Summarizing values

6.8 A worked example: Excel vs. R

6.9 Try it yourself

6.9.1 Solutions — no peeking

Terminology

Summary