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:
dates | language | interviews | interview_year | interview_year_lb | interview_month_year_lb | |
---|---|---|---|---|---|---|
1 | 2024-06-01 | Python | 119 | 2024 | 2024 | 6-2024 |
2 | 2024-06-02 | R | 99 | 2024 | 2024 | 6-2024 |
3 | 2024-06-03 | JavaScript | 222 | 2024 | 2024 | 6-2024 |
4 | 2024-06-04 | R | 224 | 2024 | 2024 | 6-2024 |
5 | 2024-06-05 | Java | 155 | 2024 | 2024 | 6-2024 |
6 | 2024-06-06 | Python | 321 | 2024 | 2024 | 6-2024 |