Dates and times are very frequently used concept in data analysis — not least for time - series study. The terrible fact is that with different numbers of days for each month, leap years, leap seconds along with time zones, they can be quite awful for dealing with programmatically.



The good news is that R provides a broad range of capabilities to deal with times and dates. While these ideas are quite fundamental to R programming, they have been left until now as some of the best ways of using them comes with in add-on packages.

In this chapter you will learn about the POSIX based Date and Time, different Date and Time classes, lubridate etc.

Date and Time Classes

There are three types of date and time classes which arrive with R programming. These are:

POSIXct
POSIXlt and
Date.

The sub categories are explained below:

POSIX Dates and Times Classes

POSIX dates and times are classic R: brightly thorough in their implementation, navigating all sorts of obscure technical issues, but with awful Unixy names that make everything seem more complicated than it really is.

The function Sys.time() is used to return the current date and time in POSIXct notation:

(now_ct <- Sys.time ())
# [1] "2016-10-28 20:48:02 BST"

Here, ct is the short form for calendar time.

Again, when the date needs to be printed, you just see a formatted version of it, so it won't go obvious how the date is stored. By using 'unclass', you can see where it is indeed just a number:

unclass (now_ct)
# [1] 1.374e+09

The Date Class

The third is the 'date' class in base R which is better named as the 'Date' class. It keeps dates as if the number of days since the starting of 1970. The 'Date' class is finely used in cases where programmers' do not bother about the time of day. Fractional days are probable which can get generated by computing a mean Date (suppose), but the POSIX classes are better for those situations like:

(now_date <- as. Date (now_ct))
## [1] "2016-10-28"
class (now_date)
## [1] "Date"
unclass(now_date)
## [1] 15903

Other classes for date and time have add-on packages which include date, dates, chron, year mon, yearqtr, timeDate, ti, and jul.

Lubridate

If you have become sad with dates and you have considered to skip the using this class, do not worry about. 'Lubridate', as the name implies, put in some much needed lubrication to the practice of date manipulation. It does not include many new features over base R, but makes your code more readable and facilitates you to avoid thinking too much.

The real beauty is dissimilar elements in the same vector be able to have different formats (as long as the year is followed by the month that is followed by the day):

library (lubridate)
# Attaching the package: 'lubridate' in your program
# The following object gets masked from 'package:chron':
# for - days, hours, minutes, seconds, years
karlos_rays_birth_date <- c(
"1994 - 08 - 28",
"1993/08\\28",
"Saturday + 1993.08*28"
)
ymd (karlos_rays _birth_date)
## [1] "1993 - 08 - 28 UTC" "1993-08-28 UTC" "1993-08-28 UTC"


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram