Reading and Transforming Data with R
September 29, 2026
| Computer | Example of an absolute pathname |
|---|---|
| Mac | /Users/USERNAME/Downloads/custdata_rev.csv |
| Windows | C:\Users\USERNAME\Downloads\custdata_rev.csv |
Windows displays the pathname with backslashes (\). When entering it in R, use forward slashes (/) instead:
\) as a special character inside quotation marks.The working directory is the folder R uses as its starting point. Run getwd() in the Console to see it.
| Role | Example |
|---|---|
| Given: working directory | /cloud/project |
| Given: CSV file’s full location | /cloud/project/data/custdata_rev.csv |
| Result: relative pathname | data/custdata_rev.csv |
custdata_rev.csv from the Class Files module in Brightspace.
custdata_rev.csv file into it.
custdatadim() shows both the number of rows and columns.nrow() and ncol() give the row count and column count separately.summary() gives a quick overview, while skimr::skim() provides a more detailed, user-friendly summary of variables across all data types.The $ operator extracts a column from a data.frame as a vector.
In this example, one observation is one customer’s spending in one month.
| Customer | Month | Spending |
|---|---|---|
| Ana | January | 42 |
| Ana | February | 38 |
| Bo | January | 55 |
Name, Age, Grade, MajorEmployeeID, Name, Age, DepartmentCustomerID, Name, Age, Income, HousingTypeNote
dplyrdplyr changes a data.frame in useful ways
dplyr is a core tidyverse package for data manipulation — tasks like filtering, sorting, selecting, and renaming.| Function | What changes? |
|---|---|
filter(DF, LOGICAL_CONDITIONS) |
Keep rows that meet a condition |
arrange(DF, VARIABLES) |
Sort rows |
distinct(DF, VARIABLES) |
Keep unique rows or combinations |
select(DF, VARIABLES) |
Select or remove columns |
rename(DF, NEW_NAME = CURRENT_NAME) |
Rename a column name |
dplyr functions take a data.frame as a first argument.
|> or %>%) passes the result on its left into the function on its right
f(x, y) is the same as x |> f(y).DF:
filter(DF, logical_condition)DF |> filter(logical_condition)DF, then keep rows where logical_condition is TRUE.dplyr verbsdplyr functions with the pipe operator:
DF |> filter(LOGICAL_CONDITIONS)DF |> arrange(VARIABLES)DF |> distinct(VARIABLES)DF |> select(VARIABLES)DF |> rename(NEW_NAME = CURRENT_NAME)dplyr functions usually take a data.frame as the first argument.DF |> filter(...) |> arrange(...)
|>) in RStudio:
|>).filter()| Row | num |
chr |
|---|---|---|
| 1 | 8 | A |
| 2 | 9 | C |
| 3 | 10 | B |
| 4 | 11 | A |
We will use this df on the next slides.
TRUE or FALSE| Condition | Meaning |
|---|---|
V1 == V2 |
Is V1 equal to V2? |
V1 != V2 |
Is V1 not equal to V2? |
V1 > V2 |
Is V1 greater than V2? |
V1 >= V2 |
Is V1 greater than or equal to V2? |
V1 < V2 |
Is V1 less than V2? |
V1 <= V2 |
Is V1 less than or equal to V2? |
V1 and V2 are variables, and the comparisons are applied element-wise (vectorized).V1 and V2 are integer or numeric.filter() keeps only the rows where the condition is TRUEx and y are logical conditions/variables.What logical operations (&, |, !) do is combining logical variables/conditions, returning a logical variable when executed.
& requires both conditions.
| requires at least one condition.
! reverses a condition.
x and y are logical conditions.
x is TRUE, it highlights the left circle.y is TRUE, it highlights the right circle.
& or a comma keeps rows that meet both conditionsfilter(), separating conditions with a comma is equivalent to combining them with the & operator.& can combine conditions on different columns| keeps rows that meet at least one conditionThe nycflights13 package supplies the flights data frame.
Each condition checks the values in its column, one row at a time.
NA means a value is missing or unknownNA (not available) represents a missing or unknown value in R.NA).na.rm = TRUE removes missing values for calculationna.rm = TRUE, both functions return NA for this vector.na.rm = TRUE removes missing values for the calculation.1 and 3, for calculation. The original data stay unchanged.v1 is Mary’s age (unknown) and v2 is John’s age (unknown).NA.is.na()is.na() to test whether a value is missing (NA).filter(), you can:
is.na() to keep observations with missing values.!is.na() to remove observations with missing values.arrange()arrange() sorts whole observationsarrange() sorts out observations.
arrange() sorts by the first variable, and then uses the next variable(s) to break ties.desc() reverses the sorting directiondesc(VARIABLE) to sort in descending order.
arrange() breaks ties in the one before itdistinct()distinct() removes repeated rowsdistinct() removes duplicate observations.
distinct() can check selected columnsdistinct().
select()select() keeps or removes variablesIt’s not uncommon to get datasets with hundreds or thousands of variables.
select() allows us to narrow in on the variables we’re actually interested in.
We can select variables by their names.
select(-VARIABLES), we can remove variables.rename() changes a variable namerename() can be used to rename variables:
DF |> rename(NEW_NAME = CURRENT_NAME)dep_delay to the clearer name departure_delay