Convert Four Digit Year Values to Class Date in R (2 Examples)

 

In this R tutorial you’ll learn how to convert a vector of years to a Date object.

The tutorial contains these contents:

Let’s get started…

 

Introducing Example Data

Let’s first create some example data:

my_dates <- seq(2020, 2025)                   # Create vector of years
my_dates                                      # Print vector of years
# [1] 2020 2021 2022 2023 2024 2025

As you can see based on the previous output of the RStudio console, our example data is a vector containing six numeric elements that correspond to a sequence of years from 2020 to 2025.

 

Example 1: Transform Year to Date Class Using ISOdate() & as.Date() Functions

The following R programming syntax demonstrates how to convert a four digit year object to a date object with the class Date.

For this, we first have to apply the ISOdate function to add months, days, and times to our data object:

my_dates1 <- ISOdate(my_dates, 1, 1)          # Convert years to date & time
my_dates1                                     # Print date & time vector
# [1] "2020-01-01 12:00:00 GMT" "2021-01-01 12:00:00 GMT"
# [3] "2022-01-01 12:00:00 GMT" "2023-01-01 12:00:00 GMT"
# [5] "2024-01-01 12:00:00 GMT" "2025-01-01 12:00:00 GMT"

Let’s check the class of our new data object:

class(my_dates1)                              # Check class
# [1] "POSIXct" "POSIXt"

At this point, our vector has the POSIXct class.

Next, we can convert our POSIXct object to a Date object using the as.Date function:

my_dates2 <- as.Date(my_dates1)               # Convert date & time to Date class
my_dates2                                     # Print Date class vector
# [1] "2020-01-01" "2021-01-01" "2022-01-01" "2023-01-01" "2024-01-01"
# [6] "2025-01-01"

As you can see, the times have been removed from our updated data object.

Let’s check the data type of this new object:

class(my_dates2)                              # Check class
# [1] "Date"

Our final data object called my_dates2 has the Date class. Looks good!

 

Example 2: Transform Year to Date Class Using ymd() Function of lubridate Package

In Example 1, I have explained how to use the functions of Base R to transform four digit years to dates.

The following R code, in contrast, illustrates how to use the lubridate package for this task.

We first have to install and load the lubridate package:

install.packages("lubridate")                 # Install & load lubridate package
library("lubridate")"

Next, we can apply the ymd function of the lubridate package to change the format of our example vector from a four digit year to the Date class:

my_dates3 <- ymd(my_dates, truncated = 2L)    # Convert year to Date class
my_dates3                                     # Print Date class vector
# [1] "2020-01-01" "2021-01-01" "2022-01-01" "2023-01-01" "2024-01-01"
# [6] "2025-01-01

Let’s also check the class:

class(my_dates3)                              # Check class
# [1] "Date"

As you can see, the output is exactly the same as in Example 1. Whether you prefer to use the basic installation or the lubridate package is a matter of taste.

 

Video & Further Resources

Have a look at the following video which I have published on my YouTube channel. In the video, I’m explaining the R codes of this article in the R programming language.

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at some of the related articles on https://www.statisticsglobe.com/. I have published several tutorials already.

 

This tutorial has shown how to transform a vector of years to a Date object in the R programming language. If you have additional questions, tell me about it in the comments below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top