create_series() allows the user to quickly create a tbl_time object with a date column populated with a sequence of dates.

create_series(
  time_formula,
  period = "day",
  class = "POSIXct",
  include_end = FALSE,
  tz = "UTC",
  as_vector = FALSE
)

Arguments

time_formula

A period to create the series over. This is specified as a formula. See the Details section of filter_time() for more information.

period

A character specification used for time-based grouping. The general format to use is "frequency period" where frequency is a number like 1 or 2, and period is an interval like weekly or yearly. There must be a space between the two.

Note that you can pass the specification in a flexible way:

  • 1 Year: '1 year' / '1 Y'

This shorthand is available for year, quarter, month, day, hour, minute, second, millisecond and microsecond periodicities.

Additionally, you have the option of passing in a vector of dates to use as custom and more flexible boundaries.

class

One of "Date", "POSIXct", "hms", "yearmon", "yearqtr". The default is "POSIXct".

include_end

Whether to always include the RHS of the time_formula even if it does not match the regularly spaced index.

tz

Time zone of the new series.

as_vector

Should the series be returned as a vector instead of a tibble?

Examples


# Every day in 2013
create_series(~'2013', 'day')
#> # A time tibble: 365 × 1
#> # Index:         date
#>    date               
#>    <dttm>             
#>  1 2013-01-01 00:00:00
#>  2 2013-01-02 00:00:00
#>  3 2013-01-03 00:00:00
#>  4 2013-01-04 00:00:00
#>  5 2013-01-05 00:00:00
#>  6 2013-01-06 00:00:00
#>  7 2013-01-07 00:00:00
#>  8 2013-01-08 00:00:00
#>  9 2013-01-09 00:00:00
#> 10 2013-01-10 00:00:00
#> # ℹ 355 more rows

# Every other day in 2013
create_series(~'2013', '2 d')
#> # A time tibble: 183 × 1
#> # Index:         date
#>    date               
#>    <dttm>             
#>  1 2013-01-01 00:00:00
#>  2 2013-01-03 00:00:00
#>  3 2013-01-05 00:00:00
#>  4 2013-01-07 00:00:00
#>  5 2013-01-09 00:00:00
#>  6 2013-01-11 00:00:00
#>  7 2013-01-13 00:00:00
#>  8 2013-01-15 00:00:00
#>  9 2013-01-17 00:00:00
#> 10 2013-01-19 00:00:00
#> # ℹ 173 more rows

# Every quarter in 2013
create_series(~'2013', '1 q')
#> # A time tibble: 4 × 1
#> # Index:         date
#>   date               
#>   <dttm>             
#> 1 2013-01-01 00:00:00
#> 2 2013-04-01 00:00:00
#> 3 2013-07-01 00:00:00
#> 4 2013-10-01 00:00:00

# Daily series for 2013-2015
create_series('2013' ~ '2015', '1 d')
#> # A time tibble: 1,095 × 1
#> # Index:         date
#>    date               
#>    <dttm>             
#>  1 2013-01-01 00:00:00
#>  2 2013-01-02 00:00:00
#>  3 2013-01-03 00:00:00
#>  4 2013-01-04 00:00:00
#>  5 2013-01-05 00:00:00
#>  6 2013-01-06 00:00:00
#>  7 2013-01-07 00:00:00
#>  8 2013-01-08 00:00:00
#>  9 2013-01-09 00:00:00
#> 10 2013-01-10 00:00:00
#> # ℹ 1,085 more rows

# Minute series for 2 months
create_series('2012-01' ~ '2012-02', 'M')
#> # A time tibble: 86,400 × 1
#> # Index:         date
#>    date               
#>    <dttm>             
#>  1 2012-01-01 00:00:00
#>  2 2012-01-01 00:01:00
#>  3 2012-01-01 00:02:00
#>  4 2012-01-01 00:03:00
#>  5 2012-01-01 00:04:00
#>  6 2012-01-01 00:05:00
#>  7 2012-01-01 00:06:00
#>  8 2012-01-01 00:07:00
#>  9 2012-01-01 00:08:00
#> 10 2012-01-01 00:09:00
#> # ℹ 86,390 more rows

# Second series for 2 minutes
create_series('2011-01-01 12:10:00' ~ '2011-01-01 12:12:00', 's')
#> # A time tibble: 121 × 1
#> # Index:         date
#>    date               
#>    <dttm>             
#>  1 2011-01-01 12:10:00
#>  2 2011-01-01 12:10:01
#>  3 2011-01-01 12:10:02
#>  4 2011-01-01 12:10:03
#>  5 2011-01-01 12:10:04
#>  6 2011-01-01 12:10:05
#>  7 2011-01-01 12:10:06
#>  8 2011-01-01 12:10:07
#>  9 2011-01-01 12:10:08
#> 10 2011-01-01 12:10:09
#> # ℹ 111 more rows

# Date class
create_series(~'2013', 'day', class = "Date")
#> # A time tibble: 365 × 1
#> # Index:         date
#>    date      
#>    <date>    
#>  1 2013-01-01
#>  2 2013-01-02
#>  3 2013-01-03
#>  4 2013-01-04
#>  5 2013-01-05
#>  6 2013-01-06
#>  7 2013-01-07
#>  8 2013-01-08
#>  9 2013-01-09
#> 10 2013-01-10
#> # ℹ 355 more rows

# yearmon class
create_series(~'2013', 'month', class = "yearmon")
#> # A time tibble: 12 × 1
#> # Index:         date
#>    date     
#>    <yearmon>
#>  1 Jan 2013 
#>  2 Feb 2013 
#>  3 Mar 2013 
#>  4 Apr 2013 
#>  5 May 2013 
#>  6 Jun 2013 
#>  7 Jul 2013 
#>  8 Aug 2013 
#>  9 Sep 2013 
#> 10 Oct 2013 
#> 11 Nov 2013 
#> 12 Dec 2013 

# hms class. time_formula specified as HH:MM:SS here
create_series('00:00:00' ~ '12:00:00', 'second' , class = "hms")
#> # A time tibble: 43,201 × 1
#> # Index:         date
#>    date  
#>    <time>
#>  1 00'00"
#>  2 00'01"
#>  3 00'02"
#>  4 00'03"
#>  5 00'04"
#>  6 00'05"
#>  7 00'06"
#>  8 00'07"
#>  9 00'08"
#> 10 00'09"
#> # ℹ 43,191 more rows

# Subsecond series
create_series('2013' ~ '2013-01-01 00:00:01', period = "10 millisec")
#> # A time tibble: 101 × 1
#> # Index:         date
#>    date               
#>    <dttm>             
#>  1 2013-01-01 00:00:00
#>  2 2013-01-01 00:00:00
#>  3 2013-01-01 00:00:00
#>  4 2013-01-01 00:00:00
#>  5 2013-01-01 00:00:00
#>  6 2013-01-01 00:00:00
#>  7 2013-01-01 00:00:00
#>  8 2013-01-01 00:00:00
#>  9 2013-01-01 00:00:00
#> 10 2013-01-01 00:00:00
#> # ℹ 91 more rows
milli <- create_series('2013' ~ '2013-01-01 00:00:01', period = ".1 sec")
# Check that 'milli' is correct by running:
# options("digits.secs" = 4)
# options("digits" = 18)
# milli$date
# as.numeric(milli$date)