In RStudio's menu: Session > Set Working Directory > Choose Directory
Navigate to a folder named "Math and Coding Camp" — create it first if it doesn't exist
Place today's datasets inside that folder
Getting Set Up
Method 2 — By code
Replace paste path here with your folder's actual location, keeping the quotation marks.
Windows
setwd("paste path here")
Right-click the folder → "Copy as path." Replace backslashes with forward slashes.
macOS
setwd("paste path here")
Option + right-click the folder → "Copy '...' as Pathname."
If R can't change the directory, the path is the problem — not the function.
Getting Set Up
Quick check
Set your working directory using either method, then run getwd() to confirm it points to the right folder.
getwd()
The One Tool You'll Use Constantly
class() — what does it do?
class() tells you what type of object you're looking at — a number, text, true/false, a full table, or something else. It's the fastest way to answer: what am I actually working with?
a <- c(4, 8, 15, 16, 23) b <- c("red", "green", "blue") d <- TRUE e <- list(count = 5, label = "sample")
class(a) class(b) class(d) class(e)
Exercise 1 — before running each line, predict what it will return, then compare.
Dataset 1 of 3
Importing life_exp
life_exp
US_Life_expectancy.csv
239 rows · 4 columns
Point and click
Environment pane → Import Dataset → From Text (base) → select the file → Import
This is the only dataset we import for now — data 2 and 3 come later.
Before You Start
Two structures, defined
Data frame — R's version of a spreadsheet: rows and columns, where each column is a vector.
Vector — a sequence of values of the same type: all numbers, or all text, but never mixed.
Dataset 1 — Task 1
What structure is life_exp?
Your Task
Determine the overall structure of life_exp, and the specific data type of its Average_Life_Expectancy column. Write your own AI prompt for this — role, task, context, format — then run it in RStudio.
str() stands for structure — it summarizes an object's type and shape in one call. Here it confirms life_exp is a data frame, and Average_Life_Expectancy is a numeric vector within it.
Dataset 1 — Task 2
Pull out a single column
Your Task
Extract the Gender column from life_exp on its own, confirm its class, and list its unique values. Write your own prompt for this.
Vectors aren't just storage — R's math functions operate on them directly, element by element.
Dataset 2 of 3
Importing air
air
Air_Quality.csv
~14,000 rows · 9 columns — NYC public health data
Point and click
Environment pane → Import Dataset → From Text (base) → select the file → Import
By code
air <- read.csv("Air_Quality.csv") head(air)
life_exp is done for now — this dataset introduces the factor.
Before You Start
One more structure: the factor
A factor is a vector with a fixed, known set of categories. It looks like text, but R treats it as belonging to a limited set of groups — which is what enables grouping, counting, and category-based charts.
Dataset 2 — Task 1
What's in each column?
Your Task
Determine which columns in air are numeric and which are text. Write your own prompt for this.
Check Your Answer
Same tool, bigger dataset
str(air)
Identify one text column whose values repeat frequently across rows — that's the one we'll use next.
Dataset 2 — Task 2
How many categories are there?
Your Task
For the repeating text column you identified, list its unique values and count how many there are. Write your own prompt for this.
Check Your Answer
Counting categories
unique(air$Name) length(unique(air$Name))
Dataset 2 — Task 3
Make it official: convert to a factor
Your Task
Convert that column to a factor. Confirm its class before and after the conversion. Write your own prompt for this.
A factor remains a vector; R additionally records that its values belong to a fixed set of categories.
Dataset 3 of 3
Importing ev
ev
Electric_Vehicle_Population_Data.xlsx
~194,000 rows · 17 columns — our first Excel file
Point and click
Environment pane → Import Dataset → From Excel → select the file → Import
By code
ev <- read_excel("Electric_Vehicle_Population_Data.xlsx") head(ev)
Same operations, much larger scale — and this one introduces the list.
Before You Start
The most flexible structure: the list
A list holds elements of different types together, without requiring a consistent row-and-column shape. A data frame requires that shape; a list does not.
Dataset 3 — Task 1
Narrow it down to one manufacturer
Your Task
Filter ev down to a single manufacturer of your choosing, using the Make column. Write your own prompt for this.
Check Your Answer
Subsetting
tesla <- subset(ev, Make == "TESLA") nrow(tesla)
Dataset 3 — Task 2
Bundle results into a list
Your Task
Using your filtered data, build a named summary containing the number of vehicles and their average electric range. Flag anything that looks like missing or unusual data. Write your own prompt for this.
A list holds elements of different types together without requiring a consistent row-and-column shape.
Dataset 3 — Task 3
Reach into the list
Your Task
Pull just the avg_range value back out of tesla_summary on its own, and independently verify it — recompute by hand or inspect a few raw rows. Write your own prompt for this.
Check Your Answer
Accessing one element
tesla_summary$avg_range
Elements inside a list are accessed with $, the same operator used for data frame columns — the syntax carries over.
Before We Wrap
Final challenge
Your Task
Pick any two of today's three datasets. Put all four structures to work: pull out a vector and confirm its type, build or use a factor, and package a result into a list. Write your own prompt for each step.
Live Share
Show us what you got
Volunteers walk us through the prompt and the result — your vector, your factor, or your list.
Recap
Four structures, one habit
Data frame — a bundle of aligned vectors
Vector — a single column or sequence of values
Factor — a vector with a fixed set of categories
List — a flexible container for elements of differing type
Carry This Forward
Same discipline as this morning: role, task, context, format — then verify. That doesn't change for the rest of today.