# Examples:
library(tidyverse)
class(6.82)[1] "numeric"
class(c)[1] "function"
class("hello")[1] "character"
class(TRUE)[1] "logical"
class(3 == 3)[1] "logical"
class(ymd(20260416))[1] "Date"
In this assignment, you’ll learn about working with dates in the tidyverse using a package called lubridate to:
Go here: https://wrds-www.wharton.upenn.edu/register/
You should get approved within a day or two. Then you’ll be able to access all the stock market data sets we’ll use in this class.
When you create an object in R, it has a class. The class tells R what kind of thing it is, and how R should treat it.
These are all classes:
# Examples:
library(tidyverse)
class(6.82)[1] "numeric"
class(c)[1] "function"
class("hello")[1] "character"
class(TRUE)[1] "logical"
class(3 == 3)[1] "logical"
class(ymd(20260416))[1] "Date"
One of the nicest features of lubridate is that it lets you turn messy-looking dates into date objects with a consistent format.
# All these return the same thing:
ymd("2026-04-16")[1] "2026-04-16"
mdy("4/16/2026")[1] "2026-04-16"
ymd(20260416)[1] "2026-04-16"
dmy(16042026)[1] "2026-04-16"
Once something is stored as a date, you can pull out useful pieces:
x <- ymd(20260131)
year(x)[1] 2026
month(x)[1] 1
month(x, label = T)[1] Jan
12 Levels: Jan < Feb < Mar < Apr < May < Jun < Jul < Aug < Sep < ... < Dec
day(x)[1] 31
wday(x)[1] 7
wday(x, label = T)[1] Sat
Levels: Sun < Mon < Tue < Wed < Thu < Fri < Sat
x + days(10)[1] "2026-02-10"
x + days(45)[1] "2026-03-17"
x + years(1)[1] "2027-01-31"
x - days(10)[1] "2026-01-21"
x - years(1)[1] "2025-01-31"
Month arithmetic can be tricky because months have different lengths. Lubridate provides %m+% and %m-% for safer month-based arithmetic.
# Example:
# 3 answers to Jan 31, 2026 + 1 month
x + months(1)[1] NA
x %m+% months(1)[1] "2026-02-28"
x + days(30)[1] "2026-03-02"
We can filter by date to get observations from only a certain window of time:
tibble(
# Sequence of dates:
date = seq.Date(
from = ymd(20260101),
to = ymd(20261231),
by = "1 day")
) %>%
filter(date < ymd(20260416))# A tibble: 105 × 1
date
<date>
1 2026-01-01
2 2026-01-02
3 2026-01-03
4 2026-01-04
5 2026-01-05
6 2026-01-06
7 2026-01-07
8 2026-01-08
9 2026-01-09
10 2026-01-10
# ℹ 95 more rows