Skip to main content

Chapter exercise: Indexing Vectors and Data Frames

Picking Out Cities and States

This exercise goes with Chapter 4, Indexing Vectors and Data Frames. It builds on the R Basics exercise from Chapter 3. It should take you about one hour. Read this whole section before you start.

TipGet the exercise file

Download indexing-exercise.Rmd

Save it in a new folder called indexing-exercise inside your geog215 folder, open it in RStudio, and work through it from top to bottom. Everything on this page is also at the top of the file.

Purpose

Skills you’ll practice. By the end of this exercise you will be able to:

  1. Index a vector by position, by a logical condition, and by name.
  2. Write comparisons (>, <, ==, !=, …) and combine them with &, |, and !.
  3. Count how many values meet a condition.
  4. Look inside a data frame with str(), nrow(), ncol(), and names().
  5. Pull out rows, columns, and single values from a data frame with [row, column] and $.
  6. Read a CSV file into R with read.csv(), and check that each column has the type you expect.

Knowledge you’ll gain. You’ll find out how North Carolina’s median income compares with the other states, and which states saw their median income fall between 2010 and 2015.

Why this matters. Most of data analysis is not making data but picking out the part you need: the counties above the state median, the tracts inside one city, the states where income fell. Every map you make this semester starts by selecting the right rows and columns. Later, the tidyverse gives you shortcuts for this (filter() and select()), but they work exactly like the brackets you’ll use today, so understanding brackets now makes the rest of the course easier.

Task

Work through Parts 1 to 7 in order, top to bottom. In each part:

  • Read the short explanation, then do each step marked with a bullet (•).
  • Wherever you see ___, replace it with the missing code before you run the chunk. A chunk with ___ in it won’t run.
  • Before you run each chunk, predict what it will return. Then run it and check. Predicting first is the fastest way to learn indexing.
  • Answer each question (Q1, Q2, …) in a sentence or two of Markdown, where it says Your answer here.
Part What you’ll do About
1 Set up your file and load the data 5 min
2 Index a vector by position 8 min
3 Index a vector with conditions, AND, OR, and NOT 12 min
4 Index a vector by name 7 min
5 Explore and index a data frame of the 50 states 18 min
6 Make a CSV file and read it into R 8 min
7 Reflect, knit, and turn in 2 min

Turn in your .Rmd file and your knitted .html file on Canvas.

Criteria for success

Use this checklist before you turn your work in. Each item matches a row of the rubric below.

Criterion Full credit Partial credit No credit
Knits (2 pts) Knits to HTML with no errors Knits, but some chunks have errors shown Doesn’t knit, or no HTML turned in
Indexing code (6 pts) Every blank filled in correctly; every chunk returns what was asked Most blanks correct Many blanks missing or wrong
Questions (8 pts) Q1 to Q12 answered correctly, using the output, with reasons Some answers missing, wrong, or with no reason Most answers missing
CSV file (2 pts) nc_cities.csv read in, with pop_2010 numeric after the fix Read in, but the type problem isn’t fixed Not done
Style (1 pt) == for equality, pipe where asked, no install.packages() Several problems
Reflection (1 pt) All three answered Missing

What a good answer looks like. Here is a question, a weak answer, and a strong one.

Question: What does nc_pop[3] return?

Weak answer: “3.”

Strong answer: “It returns 299,035, Greensboro’s 2020 population. 3 is the index, the position in the vector; the value at that position is 299,035.”

The strong answer gives the value, says what it means, and keeps the index and the value apart.

IMPORTANT! Save often. Before you knit, choose Run > Restart R and Run All Chunks to make sure every chunk works from a fresh start.