ABSTRACT

In chapter 4 we discuss R’s fast and powerful subsetting operators. Mastering them allows you to succinctly perform complex operations in a way that few other languages can match. Subsetting in R is easy to learn but hard to master because you need to internalise a number of interrelated concepts.

“Selecting multiple elements” starts by teaching you about [. You’ll learn the six ways to subset atomic vectors. You’ll then learn how those six ways act when used to subset lists, matrices, and data frames. “Selecting a single element” expands your knowledge of subsetting operators to include [[ and $ and focuses on the important principles of simplifying versus preserving. In “Subsetting and assignment” you’ll learn the art of subassignment, which combines subsetting and assignment to modify parts of an object. “Applications” leads you through eight important, but not obvious, applications of subsetting to solve problems that you often encounter in data analysis.