Chapter 5 – Indexing and Subsetting

Christine Monnier

In this chapter, we are taking our first steps into data exploration through indexing and subsetting. In both cases, this is about taking a quick look at one or more data points in our matrices or datasets to extract some information. Similarly, subsetting allows us to create smaller datasets based on the selection of data points or variables that meet specific conditions.

In addition to working with numeric matrices and dataset variables, we will also encounter logical operators and expressions again and see why they are so useful in understanding our data.

Learning Objectives

In this chapter, we will learn:

  • what indexing means;
  • how to index data points in a matrix;
  • what subsetting means;
  • how to subset elements of a dataset.

The first half of the video below focuses on indexing a matrix. The second half focuses on subsetting a dataset. Watch the video below or at the link.

This chapter concludes our first steps with the R language. In the next chapter, we will work on what to do when our code fails or returns an error message in the console.

Key functions used in this chapter

  • set.seed(): the function that determines how R selects random numbers;
  • subset(): the function that subsets datasets based on specific conditions.

Check your understanding by taking the quiz below.

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

R and R Studio For Absolute Beginners Copyright © 2022 by Christine Monnier is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.

Share This Book