How to get a year value from a date column in r?

To extract the year values from a date column in an R DataFrame, use the following code:

# with R base
your_df$your_year_colname <- format(your_df$your_date_colname, '%Y')

#with tidyverse lubridate
library(lubridate)
your_df$your_year_colname <- year(your_df$your_date_colname)

Create sample data

We will first create some a sample DataFrame that we will use in our practical example:

# define columns
dates <- seq(as.Date('06/01/2024', format = '%m/%d/%Y'), as.Date('06/06/2024' ,format = '%m/%d/%Y'),by='days')
language <- c ('Python', 'R', 'JavaScript', 'R', 'Java', 'Python')
interviews <- c (119, 99, 222, 224, 155, 321)

#initialize dataframe
hr <- data.frame (dates = dates, language = language, interviews = interviews)

Extract year value from datetime in R

Using R base, we can create a new year column using the format function as shown below:

hr$interview_year <- format(hr$dates, '%Y')

Note: using %Y as your formatter will return the full year – for example: 2024. You can get the 2 period suffix (’24) by using %y as the date formatter:

 hr$interview_year <- format(hr$dates, '%y')

Get years from dates with lubridate

Lubridate is a 3rd party package that delivers some easy to use capabilities to deal with dates on top of those delivered in base R.

Before using lubridate you need to install it in your RStudio app or JupYter Notebook. You might as well install the tidyverse package that includes lubridate.

Once installed, you can invoke lubridate in your R script to convert your date column into a new column containing years.

library(lubridate)
hr$interview_year_lb <- year(hr$dates)

Get the month and year from a date value

In the same fashion you can use lubridate to insert a month-year column as shown below:

library(lubridate)
hr$interview_month_year_lb <- paste(month(hr$dates),year(hr$dates), sep = "-")

Summary

To sum it up, below is our DataFrame with the different columns created in this tutorial:

dateslanguageinterviewsinterview_yearinterview_year_lbinterview_month_year_lb
12024-06-01Python1192024 20246-2024
22024-06-02R992024 20246-2024
32024-06-03JavaScript2222024 20246-2024
42024-06-04R2242024 20246-2024
52024-06-05Java1552024 20246-2024
62024-06-06Python3212024 20246-2024