- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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,
# Include library library("lubridate") # Print the current date print(today())
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,
# Print the current date-time object print(now())
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 −
library("lubridate") # Print the date in YY-MM-DD format # Passed as YY-MM-DD in the string ymd("2022-01-31")
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
library("lubridate") # Print the date in YY-MM-DD format # Passed as MM-DD-YY in the string mdy("01-31-2022")
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
library("lubridate") # Print the date in YY-MM-DD format # Passed as DD-MM-YY in the string dmy("31-01-2022")
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 −
# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM:SS # Passed as YY-MM-DD HH:MM:SS in the string ymd_hms("2022-01-31 22:10:10")
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 −
# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM # Passed as YY-MM-DD HH:MM in the string ymd_hm("2022-01-31 22:10")
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
# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM:SS # Passed as YY-MM-DD HH:MM:SS in the string mdy_hms("01-31-2022 22:10:10")
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
# Include library library("lubridate") # Print the date-time in YY-MM-DD HH:MM # Passed as YY-MM-DD HH:MM in the string mdy_hm("01-31-2022 22:10")
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
# Include library library("lubridate") # Print the date-time in DD-MM-YY HH:MM:SS # Passed as DD-MM-YY HH:MM:SS in the string dmy_hms("31-01-2022 22:10:10")
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
# Include library library("lubridate") # Print the date-time in DD-MM-YY HH:MM # Passed as DD-MM-YY HH:MM in the string dmy_hms("31-01-2022 22:10")
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 −
# Include library library("lubridate") # Create a date-time object in DD-MM-YY HH:MM:SS # From a string passed in the format: DD-MM-YY HH:MM:SS dateTime <- ymd_hms("2022-01-31 22:10:10") # Print the year print(paste("The year is equal to", year(dateTime)))
Output
[1] "The day of the year is equal to 31"
Print the month −
print(paste("The month is equal to", month(dateTime)))
Output
[1] "The month is equal to 1"
Print the month day −
print(paste("The day of the month is equal to", mday(dateTime)))
Output
[1] "The day of the month is equal to 31"
Print the year day −
print(paste("The day of the year is equal to", yday(dateTime)))
Output
[1] "The day of the year is equal to 31"
Print the year day −
print(paste("The day of the year is equal to", yday(dateTime)))
Output
[1] "The day of the year is equal to 31"
Print the week day
print(paste("The weekday is equal to", wday(dateTime)))
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.
- Related Articles
- Working with Dates and Times in Python
- Working with dates before 1970 in MySQL?
- How to create a vector with dates between two dates in R?
- How to create a vector with all dates in a particular year in R?
- Working with Hashtable and Dictionary in C#
- Working with lists and keys in React.js
- How to create a plot in base R with dates sequence on X-axis?
- Python - Working with Pandas and XlsxWriter
- How to Add and Subtract Dates and Times in Excel?
- Working with tag in HTML5
- Working with DateTime in C#
- Working with Images in Python?
- Working with forms in React.js
- Working with Inline CSS
- Working with CSS Units
