Determine Max & Min Dates of Vector in R (Example Code)
This article illustrates how to return the earliest and latest date of a date vector in the R programming language.
Construction of Example Data
x <- c("2025-07-13", "2023-05-07", "2022-10-11", # Example dates in R "2019-04-25", "2021-12-01", "2024-03-21", "2023-05-15", "2026-04-11", "2020-03-19") x # Display dates in RStudio console # [1] "2025-07-13" "2023-05-07" "2022-10-11" "2019-04-25" "2021-12-01" "2024-03-21" "2023-05-15" "2026-04-11" "2020-03-19" |
x <- c("2025-07-13", "2023-05-07", "2022-10-11", # Example dates in R "2019-04-25", "2021-12-01", "2024-03-21", "2023-05-15", "2026-04-11", "2020-03-19") x # Display dates in RStudio console # [1] "2025-07-13" "2023-05-07" "2022-10-11" "2019-04-25" "2021-12-01" "2024-03-21" "2023-05-15" "2026-04-11" "2020-03-19"
Example: Find Latest & Earliest Date
max(as.Date(x)) # Maximum date # [1] "2026-04-11" |
max(as.Date(x)) # Maximum date # [1] "2026-04-11"
min(as.Date(x)) # Minimum date # [1] "2019-04-25" |
min(as.Date(x)) # Minimum date # [1] "2019-04-25"