Extracting Years from Vector of Dates in R (Example Code)

This post explains how to extract years from dates in the R programming language.

Creation of Example Data

example_dates <- as.Date(c("2018/05/10", "2020/01/22", "2024/11/12"))  # Example dates
example_dates                                                          # Show example dates in console
# [1] "2018-05-10" "2020-01-22" "2024-11-12"

Example: Return Only Years from Vector of Dates

format(example_dates, "%Y")                                            # Extracting years using format()
# [1] "2018" "2020" "2024"

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top