Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Working with Dates and Times in R with lubridate
The dates and times appear simple and easy at first impression as we deal with them in our day-to-day life. But a lot of complexity involves when we work with dates and times objects in R.
This article focuses on working with dates and times using the lubridate package in R. You can install this package locally by using the following command in the CRAN?s terminal ?
install.packages("lubridate")
Types of data/time objects in R
There are three types of data/time objects and are listed below ?
Date (<data>) object ? Prints the date.
Time (<time>) object ? Prints the time.
Data-Time object (<dttm>) ? It is the combination of both data and time.
Example
R provides us today() function using which we can get the current data. For example,
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the current date <span class="token function">print</span><span class="token punctuation">(</span><span class="token function">today</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-12-13"
As you can see in the output, the current date has been displayed.
Example
R provides us now() function also that prints a data-time object. Let us consider the following example,
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Print</span> the current date<span class="token operator">-</span>time object <span class="token function">print</span><span class="token punctuation">(</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-12-13 16:25:31 IST"
As you can see in the output, the current date and time have been displayed.
Create date using a string
The lubridate library provides us helper functions using which can create a date object easily. These functions vary with each other in terms of arrangement of components (DD, MM, YY) in the passed string. Let us discuss about these functions one by one ?
Using the ymd() function
This function accepts a string as input but note that the component of the passed string must be in the format: YY MM DD.
Example
Let us consider the following program illustrating the working of this function ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD format # <span class="token class-name">Passed</span> as YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD in the string <span class="token function">ymd</span><span class="token punctuation">(</span><span class="token string">"2022-01-31"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31"
As you can see in the output, date has been printed in YY-MM-DD format.
Using the mdy() fucntion
This function accepts a string as input but note that the component of the passed string must be in the format: MM-DD-YY
Example
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD format # <span class="token class-name">Passed</span> as MM<span class="token operator">-</span>DD<span class="token operator">-</span>YY in the string <span class="token function">mdy</span><span class="token punctuation">(</span><span class="token string">"01-31-2022"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31"
As you can see in the output, date has been printed in YY- MM-DD format.
Using the dmy() function
This function accepts a string as input but note that the component of the passed string must be in the format: DD MM YY.
Example
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD format # <span class="token class-name">Passed</span> as DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY in the string <span class="token function">dmy</span><span class="token punctuation">(</span><span class="token string">"31-01-2022"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31"
As you can see in the output, date has been printed in YY-MM-DD format.
Create date-time using a string
The lubridate library provides us other helper functions also using which we can create a date-time object easily. These functions vary with each other in terms of arrangement of components (DD, MM, YY and HH, MM, SS) in the passed string. Let us discuss about these functions one by one ?
ymd_hms(date_time_string)
This function accepts a string as input but note that the component of the passed string must be in the format: YY-MM-DD HH:MM:SS.
Example
Let us consider the following program illustrating the working of this function ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date<span class="token operator">-</span>time in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS # <span class="token class-name">Passed</span> as YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS in the string <span class="token function">ymd_hms</span><span class="token punctuation">(</span><span class="token string">"2022-01-31 22:10:10"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31 22:10:10 UTC"
As you can see in the output, date has been printed in YY-MM-DD HH:MM:SS format.
ymd_hm(date_time_string)
This function accepts a string as input but note that the component of the passed string must be in the format: YY-MM-DD HH:MM.
Example
Let us consider the following program illustrating the working of this function ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date<span class="token operator">-</span>time in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM # <span class="token class-name">Passed</span> as YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM in the string <span class="token function">ymd_hm</span><span class="token punctuation">(</span><span class="token string">"2022-01-31 22:10"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31 22:10:00 UTC"
As you can see in the output, date has been printed in YY-MM-DD HH:MM format.
mdy_hms(date_time_string)
This function accepts a string as input but note that the component of the passed string must be in the format: MM DD YY
Example
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date<span class="token operator">-</span>time in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS # <span class="token class-name">Passed</span> as YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS in the string <span class="token function">mdy_hms</span><span class="token punctuation">(</span><span class="token string">"01-31-2022 22:10:10"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31 22:10:10 UTC"
As you can see in the output, date has been printed in MM-DD-YY HH:MM:SS format.
mdy_hm(date_time_string)
This function accepts a string as input but note that the component of the passed string must be in the format: MM-DD-YY HH:MM
Example
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date<span class="token operator">-</span>time in YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM # <span class="token class-name">Passed</span> as YY<span class="token operator">-</span>MM<span class="token operator">-</span>DD HH<span class="token operator">:</span>MM in the string <span class="token function">mdy_hm</span><span class="token punctuation">(</span><span class="token string">"01-31-2022 22:10"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31 22:10:00 UTC"
As you can see in the output, date has been printed in MM-DD-YY HH:MM format.
dmy_hms(date_time_string)
This function accepts a string as input but note that the component of the passed string must be in the format: DD-MM-YY HH:MM:SS.
Example
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date<span class="token operator">-</span>time in DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS # <span class="token class-name">Passed</span> as DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS in the string <span class="token function">dmy_hms</span><span class="token punctuation">(</span><span class="token string">"31-01-2022 22:10:10"</span><span class="token punctuation">)</span> </div>
Output
[1] "2022-01-31 22:10:10 UTC"
As you can see in the output, date has been printed in DD-MM-YY HH:MM:SS format.
dmy_hm(date_time_string)
This function accepts a string as input but note that the component of the passed string must be in the format: DD-MM-YY HH:MM:SS.
Example
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the date<span class="token operator">-</span>time in DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY HH<span class="token operator">:</span>MM # <span class="token class-name">Passed</span> as DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY HH<span class="token operator">:</span>MM in the string <span class="token function">dmy_hms</span><span class="token punctuation">(</span><span class="token string">"31-01-2022 22:10"</span><span class="token punctuation">)</span> </div>
Output
[1] "2020-01-31 22:22:10 UTC"
As you can see in the output, date has been printed in DD-MM-YY HH:MM format.
Get individual components of data/time
The lubridate provides us functions using which we can pull out a specific component from a date object. For example, month, date of the month, year etc. The following functions are used to extract a particular component ?
Function |
Description |
|---|---|
year() |
Pulls out the year from a date or date-time object |
month() |
Pulls out the month from a date or date-time object |
mday() |
Pulls out the day of month from a date or date-time object |
yday() |
Pulls out the day of the year from a data or date-time object |
wday() |
Pulls out the day of the week from a data or date-time object |
Example
Now let us see the working of these functions ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"># <span class="token class-name">Include</span> library <span class="token function">library</span><span class="token punctuation">(</span><span class="token string">"lubridate"</span><span class="token punctuation">)</span> # <span class="token class-name">Create</span> a date<span class="token operator">-</span>time object in DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS # <span class="token class-name">From</span> a string passed in the format<span class="token operator">:</span> DD<span class="token operator">-</span>MM<span class="token operator">-</span>YY HH<span class="token operator">:</span>MM<span class="token operator">:</span>SS dateTime <span class="token operator"><</span><span class="token operator">-</span> <span class="token function">ymd_hms</span><span class="token punctuation">(</span><span class="token string">"2022-01-31 22:10:10"</span><span class="token punctuation">)</span> # <span class="token class-name">Print</span> the year <span class="token function">print</span><span class="token punctuation">(</span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token string">"The year is equal to"</span><span class="token punctuation">,</span> <span class="token function">year</span><span class="token punctuation">(</span>dateTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "The day of the year is equal to 31"
Print the month ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token string">"The month is equal to"</span><span class="token punctuation">,</span> <span class="token function">month</span><span class="token punctuation">(</span>dateTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "The month is equal to 1"
Print the month day ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token string">"The day of the month is equal to"</span><span class="token punctuation">,</span> <span class="token function">mday</span><span class="token punctuation">(</span>dateTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "The day of the month is equal to 31"
Print the year day ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token string">"The day of the year is equal to"</span><span class="token punctuation">,</span> <span class="token function">yday</span><span class="token punctuation">(</span>dateTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "The day of the year is equal to 31"
Print the year day ?
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token string">"The day of the year is equal to"</span><span class="token punctuation">,</span> <span class="token function">yday</span><span class="token punctuation">(</span>dateTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "The day of the year is equal to 31"
Print the week day
<div class="code-mirror language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token function">print</span><span class="token punctuation">(</span><span class="token function">paste</span><span class="token punctuation">(</span><span class="token string">"The weekday is equal to"</span><span class="token punctuation">,</span> <span class="token function">wday</span><span class="token punctuation">(</span>dateTime<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span> </div>
Output
[1] "The weekday is equal to 2"
Conclusion
In this tutorial, we discussed working with dates and times in R. We saw the working various functions defined under the library lubridate. I hope this tutorial has surely contributed to your learning in data science.
