Introduction

First I download the libraries I am likely to use:

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.2.1     ✔ readr     2.2.0
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.3     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(dplyr)
library(ggplot2)
library(gt)

Data

The data for this exercise are about tuition cost in the U.S.by state. They are uploaded in GitHub, from where they are retrieved.

url <- "https://raw.githubusercontent.com/Enrique01234/607-Fall-2026/refs/heads/main/us_avg_tuition(Table%205).csv"

A data frame, “Tuition” is created.

Tuition <- read_csv(file= url, show_col_types = FALSE, progress = FALSE)

Toward a tidy data frame

Then it is long-pivoted to ensure (a) each variable is a column and each column is a variable, (b) each observation is a row and each row is an observation, and (c) each value is a cell and each cell is a single value.

Pivoted<- Tuition|>
pivot_longer (!c(State), names_to = "Year", values_to = "TuitionCost")
Pivoted
## # A tibble: 600 × 3
##    State   Year    TuitionCost
##    <chr>   <chr>   <chr>      
##  1 Alabama 2004-05 $5,683     
##  2 Alabama 2005-06 $5,841     
##  3 Alabama 2006-07 $5,753     
##  4 Alabama 2007-08 $6,008     
##  5 Alabama 2008-09 $6,475     
##  6 Alabama 2009-10 $7,189     
##  7 Alabama 2010-11 $8,071     
##  8 Alabama 2011-12 $8,452     
##  9 Alabama 2012-13 $9,098     
## 10 Alabama 2013-14 $9,359     
## # ℹ 590 more rows

However, in the original data set, the tuition cost includes the dollar sign(“$”). I separate it from the rest of the number, creating two columns in a new data frame called “Pivoted2”.

Pivoted2<- Pivoted|>
 separate(TuitionCost, c("unit", "cost"), 1)
Pivoted2
## # A tibble: 600 × 4
##    State   Year    unit  cost 
##    <chr>   <chr>   <chr> <chr>
##  1 Alabama 2004-05 $     5,683
##  2 Alabama 2005-06 $     5,841
##  3 Alabama 2006-07 $     5,753
##  4 Alabama 2007-08 $     6,008
##  5 Alabama 2008-09 $     6,475
##  6 Alabama 2009-10 $     7,189
##  7 Alabama 2010-11 $     8,071
##  8 Alabama 2011-12 $     8,452
##  9 Alabama 2012-13 $     9,098
## 10 Alabama 2013-14 $     9,359
## # ℹ 590 more rows

One additional point, which I discovered when I tried to do some calculations. The column “cost”, in spite of having being separated from the dollar sign, is still not considered a number.

In order to fix this issue, I write the data frame into a csv file.

write.csv(Pivoted2, "pivoted2.csv")

The new csv file is uploaded in GitHub. Then I bring it back here.

url <- "https://raw.githubusercontent.com/Enrique01234/607-Fall-2026/refs/heads/main/pivoted2.csv"

A new data frame, “Pivoted3” is created.

Pivoted3 <- read_csv(file= url, show_col_types = FALSE, progress = FALSE)
## New names:
## • `` -> `...1`
Pivoted3
## # A tibble: 600 × 5
##     ...1 State   Year    unit   cost
##    <dbl> <chr>   <chr>   <chr> <dbl>
##  1     1 Alabama 2004-05 $      5683
##  2     2 Alabama 2005-06 $      5841
##  3     3 Alabama 2006-07 $      5753
##  4     4 Alabama 2007-08 $      6008
##  5     5 Alabama 2008-09 $      6475
##  6     6 Alabama 2009-10 $      7189
##  7     7 Alabama 2010-11 $      8071
##  8     8 Alabama 2011-12 $      8452
##  9     9 Alabama 2012-13 $      9098
## 10    10 Alabama 2013-14 $      9359
## # ℹ 590 more rows

Now the cost column is a number and the data frame is tidy.

Using the tidy data frame

I can explore the top two states with highest tuition cost per year.

Pivoted3|>
  group_by(Year)|>
  slice_max(cost, n=2)
## # A tibble: 24 × 5
## # Groups:   Year [12]
##     ...1 State        Year    unit   cost
##    <dbl> <chr>        <chr>   <chr> <dbl>
##  1   529 Vermont      2004-05 $     11067
##  2   445 Pennsylvania 2004-05 $     10394
##  3   530 Vermont      2005-06 $     11295
##  4   446 Pennsylvania 2005-06 $     10556
##  5   531 Vermont      2006-07 $     11473
##  6   351 New Jersey   2006-07 $     10943
##  7   532 Vermont      2007-08 $     12013
##  8   352 New Jersey   2007-08 $     11427
##  9   533 Vermont      2008-09 $     12305
## 10   353 New Jersey   2008-09 $     11661
## # ℹ 14 more rows

The states that are consistently the most expensive ones in terms of tuition are:

New Hamspshire

New Jersey

Pennsylvania

Vermont

Vermont was the top one until the academic year 2010-11. Since 2011-12 the most expensive tuition cost has been New Hanpshire.

I can also explore the top two states with lowest tuition cost per year.

Pivoted3|>
  group_by(Year)|>
  slice_min(cost, n=2)
## # A tibble: 24 × 5
## # Groups:   Year [12]
##     ...1 State   Year    unit   cost
##    <dbl> <chr>   <chr>   <chr> <dbl>
##  1   325 Nevada  2004-05 $      3621
##  2    97 Florida 2004-05 $      3848
##  3   326 Nevada  2005-06 $      3687
##  4    98 Florida 2005-06 $      3924
##  5    99 Florida 2006-07 $      3888
##  6   591 Wyoming 2006-07 $      4122
##  7   100 Florida 2007-08 $      3879
##  8   592 Wyoming 2007-08 $      4072
##  9   593 Wyoming 2008-09 $      3929
## 10   101 Florida 2008-09 $      4150
## # ℹ 14 more rows

Several states appear in the list:

Alaska

Florida

Louisiana

Montana

Nevada

Utah

Wyoming

While Montana and Utah appear only once, the other countries appear several times. For instance, both Florida and Nevada appear twice as the states with the lowest tuition cost in the 2004-05 to 2007-08 period. However, since then, Wyoming has been consistently the state with the lowest cost.