6 min read

Date & Time in R - Create, Update & Verify

Introduction

This is the eigth tutorial in the series Handling Date & Time in R. In this tutorial, we will learn to create, update and verify date and time objects in R.

Resources

Below are the links to all the resources related to this tutorial:

new courses ad

Create, Update & Verify

In the second section, we learnt to create date-time objects using as.Date(), as.POSIXct() etc. In this section, we will explore a few other functions that will allow us to do the same

  • make_date()
  • make_datetime()

Create

To create date without time components, use make_date() and specify the following:

  • year
  • month
  • date

We need to specify all the components in numbers i.e. we cannot use Dec or December for the month. It has to be 12.

make_date(year  = 2019,
          month = 12,
          day   = 12)
## [1] "2019-12-12"

When you need to include time components, use make_datetime().

make_datetime(year  = 2019,
              month = 12,
              day   = 12,
              hour  = 08,
              min   = 05,
              sec   = 03,
              tz    = "UTC")
## [1] "2019-12-12 08:05:03 UTC"

Update

Let us look at another scenario. You have a date-time object and want to change one of its components i.e. any of the following

  • year
  • month
  • date

Instead of creating another date-time object, you can change any of the components using update(). In the below example, we will start with the date of release of R version 3.6.1 and using update(), we will change it to 2019-12-12.

prev_release <- ymd("2019-07-05")
prev_release %>% 
  update(year  = 2019,
         month = 12,
         mday  = 12)
## [1] "2019-12-12"

Date Sequence

So far we have created a single date-time instance. How about creating a sequence of dates? We can do that using seq.Date(). We need to specify the from date as the bare minimum input. If the end date is not specified, it will create the sequence uptil the current date.

The interval of the sequence can be specified in any of the following units:

  • day
  • week
  • month
  • quarter
  • year

We can add the following to the interval units

  • integer
  • + / - (increment or decrement)

Using the integer, we can specify multiples of the units mentioned and using the sign, we can specify whether to increment or decrement.

The below table displays the main arguments used in seq.Date():

Function Description
from Starting date of the sequence
by End date of the sequence
to Date increment of the sequence
length.out Length of the sequence
along.with Use length of this value as length of sequence

In the first example, we will create a sequence of dates from 2010-01-01 to 2019-12-31. The unit of increment should be a year i.e. the difference between the dates in the sequence should be 1 year, specified using the by argument.

seq.Date(from = as.Date("2010-01-01"), to = as.Date("2019-12-31"), by = "year")
##  [1] "2010-01-01" "2011-01-01" "2012-01-01" "2013-01-01" "2014-01-01"
##  [6] "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01"

In the next example, we change the unit of increment to a quarter i.e. the difference between the dates in the sequence should be a quarter or 3 months.

seq.Date(from = as.Date("2009-12-12"), to = as.Date("2019-12-12"), by = "quarter")
##  [1] "2009-12-12" "2010-03-12" "2010-06-12" "2010-09-12" "2010-12-12"
##  [6] "2011-03-12" "2011-06-12" "2011-09-12" "2011-12-12" "2012-03-12"
## [11] "2012-06-12" "2012-09-12" "2012-12-12" "2013-03-12" "2013-06-12"
## [16] "2013-09-12" "2013-12-12" "2014-03-12" "2014-06-12" "2014-09-12"
## [21] "2014-12-12" "2015-03-12" "2015-06-12" "2015-09-12" "2015-12-12"
## [26] "2016-03-12" "2016-06-12" "2016-09-12" "2016-12-12" "2017-03-12"
## [31] "2017-06-12" "2017-09-12" "2017-12-12" "2018-03-12" "2018-06-12"
## [36] "2018-09-12" "2018-12-12" "2019-03-12" "2019-06-12" "2019-09-12"
## [41] "2019-12-12"

We will now create a sequence of dates but instead of specifying the unit of increment, we specify the number of dates in the sequence i.e. the length of the sequence. We do this using the length.out argument which specifies the desired length of the sequence. We want the sequence to have 10 dates including the start and end date, and hence we supply the value 10 for the length.out argument.

seq.Date(from = as.Date("2010-01-01"), to = as.Date("2019-12-31"), length.out = 10)
##  [1] "2010-01-01" "2011-02-10" "2012-03-22" "2013-05-02" "2014-06-11"
##  [6] "2015-07-22" "2016-08-31" "2017-10-10" "2018-11-20" "2019-12-31"

In all of the previous examples, we have specified both the start and the end date. Let us look at a few examples where we create a sequence of dates where we only specify the start date. In the below example, we want to create a sequence of dates starting from 2010-01-01. The unit of increment should be 1 year i.e. the difference between the dates in the sequence should be 1 year and the length of the sequence should be 10 i.e. the number of dates including the start date should be 10.

seq.Date(from = as.Date("2010-01-01"), by = "year", length.out = 10)
##  [1] "2010-01-01" "2011-01-01" "2012-01-01" "2013-01-01" "2014-01-01"
##  [6] "2015-01-01" "2016-01-01" "2017-01-01" "2018-01-01" "2019-01-01"

The unit of increment can include multiples and +/- sign i.e. it can be an unit of increment or decrement. In the next example, we can increment the dates in the sequence by 2 i.e. the difference between the dates should be 2 instead of 1. This is achieved by specifying the unit of increment (multiple) first followed by a space and then the unit. In our example, it is 2 year. As you can see, the sequence now goes all the way till 2028 and the gap between the dates is 2 years.

seq.Date(from = as.Date("2010-01-01"), by = "2 year", length.out = 10)
##  [1] "2010-01-01" "2012-01-01" "2014-01-01" "2016-01-01" "2018-01-01"
##  [6] "2020-01-01" "2022-01-01" "2024-01-01" "2026-01-01" "2028-01-01"

Let us say instead of increment we want to decrement the dates i.e. the sequence of dates will go backwards as shown in the next example. We achieve this by using the - sign along with the unit of decrement. The sequence of dates in next example starts from 2010 and goes back upto 1992 and the difference between the dates in 2 years.

seq.Date(from = as.Date("2010-01-01"), by = "-2 year", length.out = 10)
##  [1] "2010-01-01" "2008-01-01" "2006-01-01" "2004-01-01" "2002-01-01"
##  [6] "2000-01-01" "1998-01-01" "1996-01-01" "1994-01-01" "1992-01-01"

In the last example, we will explore the along.with argument. Here we have supplied a vector which is a sequence of numbers from 1 to 10. The length of this vector is 10 and the same length is used as the length of the sequence i.e.  the length of value supplied to along.with is also the length of the sequence.

seq.Date(from = as.Date("2010-01-01"), by = "-2 year", along.with = 1:10)
##  [1] "2010-01-01" "2008-01-01" "2006-01-01" "2004-01-01" "2002-01-01"
##  [6] "2000-01-01" "1998-01-01" "1996-01-01" "1994-01-01" "1992-01-01"

Verify Type

How do you check if the data is a date-time object? You can do that using any of the following from the lubridate package.

  • is.Date()
  • is.POSIXct()
  • is.POSIXlt()
is.Date(release_date)
## [1] FALSE
is.POSIXct(release_date)
## [1] TRUE
is.POSIXlt(release_date)
## [1] FALSE

Your Turn

  • R 2.0.0 was released on 2004-10-04 14:24:38. Create this date using both make_date() and make_datetime()

  • R 3.0.0 was released on 2013-04-03 07:12:36. Update the date created in the previous step to the above using update()

*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: