How to Convert Dates to Years & Quarters in R (Example Code)
In this R tutorial you’ll learn how to convert date objects to years and quarters.
Example Data
x <- as.Date(c("2025-11-10", # Vector of example dates in R "2013-09-17", "2022-05-17", "2011-11-11", "2023-12-01")) x # Show example dates in RStudio console # [1] "2025-11-10" "2013-09-17" "2022-05-17" "2011-11-11" "2023-12-01" |
x <- as.Date(c("2025-11-10", # Vector of example dates in R "2013-09-17", "2022-05-17", "2011-11-11", "2023-12-01")) x # Show example dates in RStudio console # [1] "2025-11-10" "2013-09-17" "2022-05-17" "2011-11-11" "2023-12-01"
Example: Formatting Dates as Year/Quarter Using year() & quarter() Functions of lubridate Package
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate |
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate
paste0(year(x), "-0", quarter(x)) # Format dates as year/quarter # [1] "2025-04" "2013-03" "2022-02" "2011-04" "2023-04" |
paste0(year(x), "-0", quarter(x)) # Format dates as year/quarter # [1] "2025-04" "2013-03" "2022-02" "2011-04" "2023-04"