Convert Integer to Date in R (3 Examples)
In this article, I’ll explain how to change an integer to the Date class in R.
The content of the page looks as follows:
Let’s get started:
Creation of Example Data
The following data is used as basement for this R tutorial:
x <- 20221005 # Example integer value x # Print example integer # [1] 20221005
The previous output of the RStudio console shows the structure of our example data – We have created a data object containing a single integer value.
Example 1: Convert Integer to Date Using as.Date() & as.character() Functions
The following code explains how to switch from the integer to the Date class using the as.Date and as.character functions.
Have a look at the following R code:
x_date1 <- as.Date(as.character(x), # as.Date & as.character functions format = "%Y%m%d") x_date1 # Print date # [1] "2022-10-05"
As you can see, we have created a new data object that is properly formatted as a date, i.e. the fifth of October 2022.
Example 2: Convert Integer to Date Using strptime() Function
In Example 2, I’ll demonstrate how to strptime function to convert an integer value to a properly formatted date:
x_date2 <- strptime(x, format = "%Y%m%d") # strptime function x_date2 # Print date # [1] "2022-10-05 UTC"
The previous output is slightly different compared to the output of Example 1: The strptime function does also add the time zone to our date object.
Example 3: Convert Integer to Date Using ymd() Function of lubridate Package
This example shows how to use the ymd function of the lubridate package to create a date object.
We first have to install and load the lubridate package, in order to use the functions that are contained in the package:
install.packages("lubridate") # Install lubridate package library("lubridate") # Load lubridate
Next, we can apply the ymd function as shown below:
x_date3 <- ymd(x) # ymd function x_date3 # Print date # [1] "2022-10-05"
Looks good!
Video, Further Resources & Summary
Do you need more information on the R code of this tutorial? Then I recommend having a look at the following video that I have published on the Statistics Globe YouTube channel. I demonstrate the contents of this tutorial in the video.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might read the other articles on my website. You can find some tutorials below.
- Convert UNIX Timestamp to Date Object in R
- Convert Data Frame Column to Numeric in R
- Convert Date to Numeric Time Object in R
- Convert Data Frame with Date Column to Time Series Object
- Introduction to R
This article has shown how to convert an integer to the Date class in the R programming language. If you have any further questions, let me know in the comments section below. Furthermore, please subscribe to my email newsletter in order to get updates on the newest posts.
Statistics Globe Newsletter