How to Create Date Sequences in R

How to create date sequences in R

To create a sequence of dates in R, we can use the seq() function, which uses the following syntax:

seq(from, to, by, length.out)

  • from – starting date
  • to – ending date
  • by – time period to increment by
  • length.out – desired length of sequence

The following examples show several ways to create date sequences:

Date Sequences of Days

seq(as.Date('2019-01-01'), as.Date('2019-01-05'), by = 1)

#[1] "2019-01-01" "2019-01-02" "2019-01-03" "2019-01-04" "2019-01-05"

seq(as.Date('2019-01-01'), as.Date('2019-01-05'), by = 'days')
#[1] "2019-01-01" "2019-01-02" "2019-01-03" "2019-01-04" "2019-01-05"

seq(as.Date('2019/1/1'), as.Date('2019/1/5'), by = '2 days') 

#[1] "2019-01-01" "2019-01-03" "2019-01-05"

Date Sequences of Weeks, Months, Quarters, Years

seq(as.Date('2019-01-01'), as.Date('2019-01-29'), by = 'weeks')

#[1] "2019-01-01" "2019-01-08" "2019-01-15" "2019-01-22" "2019-01-29"

seq(as.Date('2019-01-01'), as.Date('2019-12-01'), by = 'months') 

#[1] "2019-01-01" "2019-02-01" "2019-03-01" "2019-04-01" "2019-05-01"
#[6] "2019-06-01" "2019-07-01" "2019-08-01" "2019-09-01" "2019-10-01"
#[11] "2019-11-01" "2019-12-01"

seq(as.Date('2019-01-01'), as.Date('2019-12-01'), by = 'quarters') 

#[1] "2019-01-01" "2019-04-01" "2019-07-01" "2019-10-01"

seq(as.Date('2010/1/1'), as.Date('2019/1/1'), by = 'years') 

#[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"

Date Sequences with Time

We can also create date sequences that include time, except we need to to use as.POSIXct instead of as.Date when creating our date objects:

seq(as.POSIXct('2019-1-1 0:00'), as.POSIXct('2019-1-1 07:00'), by = 'hour')

#[1] "2019-01-01 00:00:00 UTC" "2019-01-01 01:00:00 UTC"
#[3] "2019-01-01 02:00:00 UTC" "2019-01-01 03:00:00 UTC"
#[5] "2019-01-01 04:00:00 UTC" "2019-01-01 05:00:00 UTC"
#[7] "2019-01-01 06:00:00 UTC" "2019-01-01 07:00:00 UTC"

seq(as.POSIXct('2019-1-1 0:00'), as.POSIXct('2019-1-1 1:00'), by = '5 min') 

#[1] "2019-01-01 00:00:00 UTC" "2019-01-01 00:05:00 UTC"
#[3] "2019-01-01 00:10:00 UTC" "2019-01-01 00:15:00 UTC"
#[5] "2019-01-01 00:20:00 UTC" "2019-01-01 00:25:00 UTC"
#[7] "2019-01-01 00:30:00 UTC" "2019-01-01 00:35:00 UTC"
#[9] "2019-01-01 00:40:00 UTC" "2019-01-01 00:45:00 UTC"
#[11] "2019-01-01 00:50:00 UTC" "2019-01-01 00:55:00 UTC"
#[13] "2019-01-01 01:00:00 UTC"

Date Sequences of Specified Length

We can also create date sequences that have specified lengths. For example, the following code specifies the start date as January 1, 2019 and the end date as March 1, 2019, then requests R to find 6 equally spaced dates between these two dates:

seq(as.Date('2019-01-01'), as.Date('2019-03-01'), length.out = 6)

#[1] "2019-01-01" "2019-01-12" "2019-01-24" "2019-02-05" "2019-02-17"
#[6] "2019-03-01"

Leave a Reply

Your email address will not be published. Required fields are marked *