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_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" |
format(example_dates, "%Y") # Extracting years using format() # [1] "2018" "2020" "2024"