Skip to contents

A function that extracts the year from dates where the date can be in such formats as: DD/MM/YYYY, YYYY-MM-DD, MM/YYYY, YYYY-MM, YYYY, DD Month YYYY.

Usage

extract_year(dates)

Arguments

dates

A vector of dates.

Value

A vector of corresponding years.

Details

The method works by extracting any patterns of 4 numbers in a row. Thus, the year must be in the format YYYY. I.e. cannot have formats such as DD/MM/YY.

Examples

extract_year(c('03/05/2021', '3rd of May 2022', '2023-05-03'))
#> [1] 2021 2022 2023