EPPS Math & Coding Camp

R, RStudio &
Data Structures

Vectors, factors, data frames, and lists.

JP AdjadehEPPS, UT Dallas
Before We Begin

Morning session check-in

Attendance check-in QR code
Getting Set Up

Installing R

Windows

  1. Go to the R Project website (r-project.org)
  2. Select "Download R for Windows," then "base"
  3. Download and run the installer for the latest version

macOS

  1. Go to the R Project website (r-project.org)
  2. Select "Download R for macOS"
  3. Download the .pkg file for your macOS version and follow the prompts
Getting Set Up

Installing RStudio

  1. Go to the RStudio download page (posit.co/download/rstudio-desktop)
  2. Select "RStudio Desktop"
  3. Download the installer for your operating system
Quick Check — open RStudio. In the console, type version and press Enter. That's the R version you're running today.
Getting Set Up

Installing a package manager

pacman simplifies installing and loading R packages. In the console, run:

install.packages("pacman")
library(pacman)
p_load(readxl, ggplot2)
Getting Set Up

Setting the working directory

This tells R where to look for files by default.

Method 1 — Point and click

  1. In RStudio's menu: Session > Set Working Directory > Choose Directory
  2. Navigate to a folder named "Math and Coding Camp" — create it first if it doesn't exist
  3. 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

By code

life_exp <- read.csv("US_Life_expectancy.csv")
head(life_exp)

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.
Check Your Answer

str() and class()

str(life_exp)
class(life_exp)
class(life_exp$Average_Life_Expectancy)
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.
Check Your Answer

A column, on its own

life_exp$Gender
class(life_exp$Gender)
unique(life_exp$Gender)
A single column pulled out of a data frame is just a vector — R doesn't treat it any differently once it's on its own.
Dataset 1 — Task 3

Do something with the vector

Your Task
Compute the average and the range of the Average_Life_Expectancy vector. Write your own prompt for this.
Check Your Answer

Vectors support math directly

mean(life_exp$Average_Life_Expectancy, na.rm = TRUE)
range(life_exp$Average_Life_Expectancy, na.rm = TRUE)
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.
Check Your Answer

Before and after

class(air$Name)
air$Name <- factor(air$Name)
class(air$Name)
levels(air$Name)
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.
Check Your Answer

Building the list

tesla_summary <- list(
  count = nrow(tesla),
  avg_range = mean(tesla$Electric_Range, na.rm = TRUE)
)
class(tesla_summary)
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