Blog

This article explores the processing of Date & Time in R using lubridate package.

There are broadly 3 types of Date & Time data types in R - 
date
A time
A date-time

For this article we will be using the R package called 'lubridate'.

lubridate

lubridate is not available as base R package and thus has to be installed before it can be used 

This package makes working with Date & Time fun in R .

Before , we can use lubridate , we have to install the lubridate package :
install.packages('lubridate' , dependencies = T)

Next , we have to load the package to memory :
require(lubridate)


Various functions / features available in lubridate 

1) today()
This function returns today's date .

Example 1 : To get today's date 
> today() 
[1] "2018-12-08"

Example 2 : To get individual parts of the date ( year , month & day )
Get the Year from Date
> year(today())
[1] 2018

Get the Month number from Date
> month(today())
[1] 12

Get the Month name from Date
> month(today() , label = T)
[1] Dec
12 Levels: Jan < Feb < Mar < Apr < ... < Dec

Get the day number of the month from Date
> day(today())
[1] 8

Get the day number of the week from Date
> wday(today())
[1] 4

Get the weekday name from Date
> wday(today() , label = T)
[1] Wed
7 Levels: Sun < Mon < Tue < Wed < ... < Sat

Get the day number of the year from Date
> yday(today())
[1] 346

Example 3 : To get day of the week from Date ( where 1 = Sunday & 2 = Monday ....)
> wday(today())
[1] 7

Example 4 : To get day of the week from Date with name of the week as output 
> wday(today() , label = T)
[1] Sat
7 Levels: Sun < Mon < Tue < Wed < ... < Sat

2) now()
This function returns the date-time representing this exact moment in time

Example 1 : To get date & time now
> now()
[1] "2018-12-08 18:17:23 IST"

Example 2 : To get individual component of time 
> hour(now()) 
[1] 18 
> minute(now()) 
[1] 25 
> second(now()) 
[1] 28.55923

Example 3 : To get date only from date_time variable
> date(now())
[1] "2018-12-12"



3) Parsing Date & Time strings into Date or Time datatype ( Take a date/time string and convert into date/time data type )

Any string containing Date can be converted into a DATE datatype by using adequate combination of 'y' or 'm' or 'd' to parse a Date string into Date Data type.

Example 1 : - Parsing a Date string which is having Year followed by Month and finally Day 
> ymd("20181210") 
[1] "2018-12-10" 
> class(ymd("20181210")) 
[1] "Date"

Example 2 : - Parsing a Date string which is having Day followed by Month and finally Year 
> dmy("10122018") 
[1] "2018-12-10" 
> class(dmy("10122018")) 
[1] "Date"

Example 3 : - Parsing a Date string which is having some delimiters in between
> dmy("10//12/2018") 
[1] "2018-12-10" 
> class(dmy("10//12//2018")) 
[1] "Date"

Example 4 : - Parsing a Date & Time string which is having some delimiters in between
> dmy_hms("10/12/2018 15:12:40") 
[1] "2018-12-10 15:12:40 UTC"

Example 5 : - Parsing a Time string 
> hms("21:45:30") 
[1] "21H 45M 30S"


4) Check for Leap Year  using the leap_year() : 
leap_year() will return TRUE or FALSE based on whether the year in the date is a leap year or not

Example 1 : To check for Leap Year from a Date Variable
> today()
[1] "2018-12-12"
> leap_year(today())
[1] FALSE

Example 2 : To check for Leap Year from a Date_Time Variable
> now()
[1] "2018-12-12 20:58:28 IST"
> leap_year(now())
[1] FALSE

Example 3 : To check for Leap Year 
> leap_year(ymd("2000/01/12")) 
[1] TRUE


5) To find the Time Zone defined for your System / R Environment : Sys.timezone()

Example 3 : sys.timezone() is from base package 
> Sys.timezone()
[1] "Asia/Calcutta"


6) To check whether the time is in 'am' or 'pm' : using am() / pm() functions
the am() / pm() function will take time as input and return True or False 

Example 1 : To check whether the time occurs in am 
> am(now()) 
[1] FALSE

Example 2 : To check whether the time occurs in pm 
> pm(now()) 
[1] TRUE

7) To find the difference between 2 dates in terms of years / months / days ...

Example :- 
> as.numeric(as.duration(ymd("2018/12/12") - ymd("2003/12/06")), "years")
[1] 15.01711
> as.numeric(as.duration(ymd("2018/12/12") - ymd("2003/12/06")), "months")
[1] 180.2053
> as.numeric(as.duration(ymd("2018/12/12") - ymd("2003/12/06")), "days")
[1] 5485



Thanks & Happy Learning
Priyaranjan Mohanty
@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment