In this new series Handling Date & Time in R, we will learn to handle date & time in R. We will start off by learning how
to get current date & time before moving on to understand how R handles date/time internally
and the different classes such as
POSIXct/lt. We will spend some time
exploring time zones, daylight savings and ISO 8001 standard for representing date/time.
We will look at all the weird formats in which date/time come in real world and learn to
parse them using conversion specifications. After this, we will also learn how to handle date/time
columns while reading external data into R. We will learn to extract and update different date/time
components such as year, month, day, hour, minute etc., create sequence of dates in different ways
and explore intervals, durations and period. We will end the tutorial by learning how to round/rollback
dates. Throughout the series, we will also work through a case study to better understand the
concepts we learn. Happy learning!
Below are the links to all the resources related to this tutorial:
Let us begin by looking at the current date and time.
today() will return the current date.
Sys.Date() ##  "2021-02-03" lubridate::today() ##  "2021-02-03"
now() return the date, time and the timezone. In
now(), we can specify the timezone using the
Sys.time() ##  "2021-02-03 18:54:23 IST" lubridate::now() ##  "2021-02-03 18:54:23 IST" lubridate::now(tzone = "UTC") ##  "2021-02-03 13:24:23 UTC"
AM or PM?
pm() allow us to check whether date/time occur in the
PM? They return a logical value i.e.
lubridate::am(now()) ##  FALSE lubridate::pm(now()) ##  TRUE
We can also check if the current year is a leap year using
Sys.Date() ##  "2021-02-03" lubridate::leap_year(Sys.Date()) ##  FALSE
||Whether time occurs in am?|
||Whether time occurs in pm?|
||Check if the year is a leap year?|
- get current date
- get current time
- check whether the time occurs in am or pm?
- check whether the following years were leap years
Throughout the tutorial, we will work on a case study related to transactions of an imaginary trading company. The data set includes information about invoice and payment dates.
transact <- readr::read_csv('https://raw.githubusercontent.com/rsquaredacademy/datasets/master/transact.csv') ## # A tibble: 2,466 x 3 ## Invoice Due Payment ##
## 1 2013-01-02 2013-02-01 2013-01-15 ## 2 2013-01-26 2013-02-25 2013-03-03 ## 3 2013-07-03 2013-08-02 2013-07-08 ## 4 2013-02-10 2013-03-12 2013-03-17 ## 5 2012-10-25 2012-11-24 2012-11-28 ## 6 2012-01-27 2012-02-26 2012-02-22 ## 7 2013-08-13 2013-09-12 2013-09-09 ## 8 2012-12-16 2013-01-15 2013-01-12 ## 9 2012-05-14 2012-06-13 2012-07-01 ## 10 2013-07-01 2013-07-31 2013-07-26 ## # ... with 2,456 more rows
We will explore more about reading data sets with date/time columns after learning how to parse date/time. We have shared the code for reading the data sets used in the practice questions both in the Learning Management System as well as in our GitHub repo.
The data set has 3 columns. All the dates are in the format (yyyy-mm-dd).
In the case study, we will try to answer a few questions we have about the
- extract date, month and year from Due
- compute the number of days to settle invoice
- compute days over due
- check if due year is a leap year
- check when due day in february is 29, whether it is a leap year
- how many invoices were settled within due date
- how many invoices are due in each quarter
*As the reader of this blog, you are our most important critic and commentator. We value your opinion and want to know what we are doing right, what we could do better, what areas you would like to see us publish in, and any other words of wisdom you are willing to pass our way.
We welcome your comments. You can email to let us know what you did or did not like about our blog as well as what we can do to make our post better.*
Email: [email protected]