Read + filter · Week 06
Reading and Transforming Data with R
Bring a CSV into R, inspect its rows and columns, and use filter() to keep the observations needed for a question.
Sep 28–Oct 2Lecture 5 through filter() + Classwork 5
ImportantWeek 6 sequence
- Use Lecture 5 to read a CSV from a project folder or web address and inspect the resulting data frame.
- Use Classwork 5 to find and copy an absolute pathname for a downloaded file.
- Use the pipe and
filter()to keep rows that meet logical conditions.
We finished Lecture 5 through filter() this week. We will continue with arrange(), distinct(), select(), and rename(), and complete Classworks 6 and 7, in Week 7.
Focus
- CSV files and absolute versus relative paths
- Rows, columns, observations, and variables
- The pipe and filtering rows by condition
Prepare
- Open Lecture 5 and Classwork 5
- Open your section’s project in Posit Cloud
- Download the lecture’s CSV from Brightspace
Practice
- Read a CSV with read_csv() and inspect its data frame
- Find and copy a downloaded file’s pathname
- Keep rows with filter() and combine conditions
Learning targets
- Explain what a CSV stores and how an absolute or relative pathname locates a file.
- Use
read_csv()to import a CSV from a project folder or web address. - Inspect a data frame’s dimensions, variable names, and values.
- Connect steps with the pipe and use
filter()to keep rows that meet a condition. - Combine filtering conditions with AND (
&or a comma), OR (|), and NOT (!). - Recognize missing values and check them with
is.na().
From a file to a useful data frame
| Concept | Plain-language meaning |
|---|---|
| CSV | A plain-text table with values separated by commas. |
| Pathname | The location of a file, written as an absolute or relative path. |
| Data frame | A table in R with observations in rows and variables in columns. |
Pipe (\|>) |
Pass the data from one step into the next function. |
filter() |
Keep rows where a logical condition is TRUE. |
Tip
In Posit Cloud, put custdata_rev.csv in a data folder inside your project. Then data/custdata_rev.csv is a relative pathname from the project folder.
Core pattern
custdata <- readr::read_csv("data/custdata_rev.csv")
dplyr::glimpse(custdata)Lecture slides
TipLecture slide shortcuts
Click inside the slide preview, then use:
- the visible ←/→ controls to page through the slides;
- your mouse wheel or trackpad to keep scrolling the weekly page;
- M to open the menu;
- F to enter fullscreen;
- E to enter or exit PDF export mode;
- Ctrl + Shift + F to search within the slides;
- Esc to exit the menu or fullscreen; and
- T to switch between dark and light mode.