wk5-d02-data-classes
Chico State
DATA 385 - Fall 2026
September 23, 2026
🎥 Data classes - code along in wk5-d02
New Packages
glue: glue() makes it easy to interpolate data into strings:
We talked about types so far, next we’ll introduce the concept of classes.
R uses factors to handle categorical variables — variables that have a fixed and known set of possible values.
We can think of factors as character (level labels) and integer (level numbers) glued together.
Looks like a number but acts like a date?
Dates are stored as “number of days since some origin”.
We can think of data frames as vectors of equal length glued together.
Lists are a generic vector container — vectors of any type can go in them.
pull() function, we extract a vector from the data frameHow does this differ from df$y or df[,'y']?
… stay for the logo

See more examlpes at: https://forcats.tidyverse.org/ and/or Math 130 https://math130.netlify.app/factors
# A tibble: 119,390 × 4
arrival_date_year arrival_date_month arrival_date_week_number
<dbl> <chr> <dbl>
1 2015 July 27
2 2015 July 27
3 2015 July 27
4 2015 July 27
5 2015 July 27
6 2015 July 27
7 2015 July 27
8 2015 July 27
9 2015 July 27
10 2015 July 27
# ℹ 119,380 more rows
# ℹ 1 more variable: arrival_date_day_of_month <dbl>
# A tibble: 119,390 × 33
arrival_date hotel is_canceled lead_time arrival_date_year
<glue> <chr> <dbl> <dbl> <dbl>
1 2015 July 1 Resort Hotel 0 342 2015
2 2015 July 1 Resort Hotel 0 737 2015
3 2015 July 1 Resort Hotel 0 7 2015
4 2015 July 1 Resort Hotel 0 13 2015
5 2015 July 1 Resort Hotel 0 14 2015
6 2015 July 1 Resort Hotel 0 14 2015
7 2015 July 1 Resort Hotel 0 0 2015
8 2015 July 1 Resort Hotel 0 9 2015
9 2015 July 1 Resort Hotel 1 85 2015
10 2015 July 1 Resort Hotel 1 75 2015
# ℹ 119,380 more rows
# ℹ 28 more variables: arrival_date_month <chr>,
# arrival_date_week_number <dbl>, arrival_date_day_of_month <dbl>,
# stays_in_weekend_nights <dbl>, stays_in_week_nights <dbl>,
# adults <dbl>, children <dbl>, babies <dbl>, meal <chr>,
# country <chr>, market_segment <chr>, distribution_channel <chr>,
# is_repeated_guest <dbl>, previous_cancellations <dbl>, …
# A tibble: 793 × 2
arrival_date n
<glue> <int>
1 2015 August 1 110
2 2015 August 10 207
3 2015 August 11 117
4 2015 August 12 133
5 2015 August 13 107
6 2015 August 14 329
7 2015 August 15 190
8 2015 August 16 98
9 2015 August 17 188
10 2015 August 18 94
# ℹ 783 more rows
Why does the plot start with August when we know our data start in July? And why does 10 August come after 1 August?
The dates are still character strings — they sort alphabetically, not chronologically.
# A tibble: 119,390 × 33
arrival_date hotel is_canceled lead_time arrival_date_year
<date> <chr> <dbl> <dbl> <dbl>
1 2015-07-01 Resort Hotel 0 342 2015
2 2015-07-01 Resort Hotel 0 737 2015
3 2015-07-01 Resort Hotel 0 7 2015
4 2015-07-01 Resort Hotel 0 13 2015
5 2015-07-01 Resort Hotel 0 14 2015
6 2015-07-01 Resort Hotel 0 14 2015
7 2015-07-01 Resort Hotel 0 0 2015
8 2015-07-01 Resort Hotel 0 9 2015
9 2015-07-01 Resort Hotel 1 85 2015
10 2015-07-01 Resort Hotel 1 75 2015
# ℹ 119,380 more rows
# ℹ 28 more variables: arrival_date_month <chr>,
# arrival_date_week_number <dbl>, arrival_date_day_of_month <dbl>,
# stays_in_weekend_nights <dbl>, stays_in_week_nights <dbl>,
# adults <dbl>, children <dbl>, babies <dbl>, meal <chr>,
# country <chr>, market_segment <chr>, distribution_channel <chr>,
# is_repeated_guest <dbl>, previous_cancellations <dbl>, …
ymd() is the function from lubridate package that is used here.
# A tibble: 793 × 2
arrival_date n
<date> <int>
1 2015-07-01 122
2 2015-07-02 93
3 2015-07-03 56
4 2015-07-04 88
5 2015-07-05 53
6 2015-07-06 75
7 2015-07-07 54
8 2015-07-08 69
9 2015-07-09 80
10 2015-07-10 51
# ℹ 783 more rows
Note
This page adapts material from Data Science in a Box (Unit 2, Deck 11: “Data classes”) by Mine Çetinkaya-Rundel, licensed under CC BY-SA 4.0. Source: tidyverse/datascience-box. Modified: converted from xaringan to Quarto revealjs; reads hotels.csv from a local file instead of downloading it.