R: Use lubridate to Get and Set Current Year


Often you may want to either get the current year or set the current year in R.

Fortunately this is easy to do by using the year() function from the lubridate package in R, which is designed to perform this exact task.

The year() function uses the following basic syntax:

year(x)

where:

  • x: The name of a date-time object

The following examples show how to use the year() function from the lubridate package in practice with the following data frame named df that contains information about various employees at some company:

#create data frame
df <- data.frame(start_date=c('2022-01-03', '2022-02-15', '2023-05-09',
                              '2023-08-10', '2024-10-14', '2024-12-30'),
                 end_date=c('2022-01-09', '2024-02-15', '2024-05-19',
                              '2024-03-10', '2024-12-14', '2025-11-30'),
                 sales=c(130, 98, 120, 88, 94, 100))

#view data frame
df

  start_date   end_date sales
1 2022-01-03 2022-01-09   130
2 2022-02-15 2024-02-15    98
3 2023-05-09 2024-05-19   120
4 2023-08-10 2024-03-10    88
5 2024-10-14 2024-12-14    94
6 2024-12-30 2025-11-30   100

This data frame contains the following columns:

  • start_date: The date that the employee first started working at the company
  • end_date: The date that the employee stopped working at the company
  • sales: The total sales made by the employee during their tenure

Note: Before using the year() function, you may need to first install the lubridate package by using the following syntax:

install.packages('lubridate')

Once the lubridate package is installed, you can use the year() function.

Example 1: Use Lubridate to Get Current Year

Suppose that we would like to use the lubridate package to get the current year.

We can use the following syntax to do so:

library(lubridate)

#get current year
lubridate::year(Sys.Date())

[1] 2024

This returns 2024, which represents the current year that this article is being written.

Note that the Sys.Date() function from base R returns the current date and we use the year() function from the lubridate package to simply extract the year from this date.

If we’d like, we can use the following syntax to add a new column to the data frame that contains the current year:

library(lubridate)

#add new column to data frame that contains current year
df$cur_year <- lubridate::year(Sys.Date())

  start_date   end_date sales cur_year
1 2022-01-03 2022-01-09   130     2024
2 2022-02-15 2024-02-15    98     2024
3 2023-05-09 2024-05-19   120     2024
4 2023-08-10 2024-03-10    88     2024
5 2024-10-14 2024-12-14    94     2024
6 2024-12-30 2025-11-30   100     2024

Notice that a new column named cur_year has been added to the data frame that contains the value 2024 for each row.

Example 2: Use Lubridate to Compare Current Year to Given Date

Suppose that we would like to use the lubridate package to compare each date in the end_date column to the current year.

We can use the following syntax to do so:

library(lubridate)

#compare end date to current year
df$before_current <- df$end_date < lubridate::year(Sys.Date())

#view updated data frame
df

  start_date   end_date sales before_current
1 2022-01-03 2022-01-09   130           TRUE
2 2022-02-15 2024-02-15    98          FALSE
3 2023-05-09 2024-05-19   120          FALSE
4 2023-08-10 2024-03-10    88          FALSE
5 2024-10-14 2024-12-14    94          FALSE
6 2024-12-30 2025-11-30   100          FALSE

Notice that this creates a new column named before_current that checks if the corresponding date in the end_date column occurs before the current year.

A value of TRUE indicates that the date in the end_date column occurs before the current year of 2024.

A value of FALSE indicates that the date in the end_date column occurs on or after the current year of 2024.

Note: You can find the complete documentation for the year() function from the lubridate package here.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Generate a Sequence of Dates with Lubridate in R
How to Convert Character to Date Using Lubridate in R
How to Get First or Last Day of Month Using Lubridate in R

Leave a Reply

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