How to Use the year() Function in R


Often you may want to extract only the year component of a date object 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 example shows how to use the year() function from the lubridate package in practice.

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: How to Use the year() Function in R

Suppose we create the following data frame named df that contains information about total sales made on various dates by some company:

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

#view data frame
df

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

Suppose that we would like to extract only the year from each date in the date column of the data frame.

We can use the year() function from the lubridate package to do so:

library(lubridate)

#extract year from each date in 'date' column
year(df$date)

[1] 2022 2022 2023 2023 2024 2024

This returns a vector of years from each date in the date column.

We could also use the following syntax to create a new column in the data frame to hold these dates:

library(lubridate)

#create new column that only contains year from each date in 'date' column
df$year_date <- year(df$date)

#view updated data frame
df

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

Notice that the new column named year_date now contains only the year portion from each date in the date column.

We can also use comparison operators with the year() function to determine if the year portion of each date is greater than or less than a specific year.

For example, we can use the following syntax to check if the year in each date in the date column is greater than 2023:

library(lubridate)

#check if each date in 'date' column is greater than 2023
year(df$date) > 2023

[1] FALSE FALSE FALSE FALSE  TRUE  TRUE

This returns a vector of TRUE or FALSE values to indicate if the year in each date is greater than 2023.

We can also use the following syntax to assign these values to a new column in the data frame:

library(lubridate)

#create new column that checks if the year of each date is greater than 2023
df$year_greater <- year(df$date) > 2023 

#view updated data frame
df

        date sales year_greater
1 2022-01-03   130        FALSE
2 2022-02-15    98        FALSE
3 2023-05-09   120        FALSE
4 2023-08-10    88        FALSE
5 2024-10-14    94         TRUE
6 2024-12-30   100         TRUE

Notice that the new column named year_greater now returns TRUE or FALSE to indicate whether or not the corresponding year in the date column is greater than 2023 or not.

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 *