time_seq {timeplyr} | R Documentation |
Time based version of base::seq()
Description
Time based version of base::seq()
Usage
time_seq(
from,
to,
time_by,
length.out = NULL,
time_type = getOption("timeplyr.time_type", "auto"),
week_start = getOption("lubridate.week.start", 1),
time_floor = FALSE,
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "NA")
)
time_seq_sizes(
from,
to,
time_by,
time_type = getOption("timeplyr.time_type", "auto")
)
time_seq_v(
from,
to,
time_by,
time_type = getOption("timeplyr.time_type", "auto"),
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "NA"),
time_floor = FALSE,
week_start = getOption("lubridate.week.start", 1)
)
time_seq_v2(
sizes,
from,
time_by,
time_type = getOption("timeplyr.time_type", "auto"),
time_floor = FALSE,
week_start = getOption("lubridate.week.start", 1),
roll_month = getOption("timeplyr.roll_month", "preday"),
roll_dst = getOption("timeplyr.roll_dst", "NA")
)
Arguments
from |
Start date/datetime of sequence. |
to |
End date/datetime of sequence. |
time_by |
Time unit increment.
|
length.out |
Length of the sequence. |
time_type |
If "auto", |
week_start |
day on which week starts following ISO conventions - 1
means Monday (default), 7 means Sunday.
This is only used when |
time_floor |
Should |
roll_month |
Control how impossible dates are handled when
month or year arithmetic is involved.
Options are "preday", "boundary", "postday", "full" and "NA".
See |
roll_dst |
See |
sizes |
Time sequence sizes. |
Details
This works like seq()
,
but using timechange
for the period calculations and
base::seq.POSIXT()
for the duration calculations.
In many ways it is improved over seq
as
dates and/or datetimes can be supplied with no errors to
the start and end points.
Examples like,
time_seq(now(), length.out = 10, by = "0.5 days", seq_type = "dur")
and
time_seq(today(), length.out = 10, by = "0.5 days", seq_type = "dur")
produce more expected results compared to
seq(now(), length.out = 10, by = "0.5 days")
or
seq(today(), length.out = 10, by = "0.5 days")
.
For a vectorized implementation with multiple start/end times,
use time_seq_v()
/time_seq_v2()
time_seq_sizes()
is a convenience
function to calculate time sequence lengths, given start/end times.
Value
time_seq
returns a time sequence.
time_seq_sizes
returns an integer vector of sequence sizes.
time_seq_v
returns time sequences.
time_seq_v2
also returns time sequences.
See Also
Examples
library(timeplyr)
library(lubridate)
# Dates
today <- today()
now <- now()
time_seq(today, today + years(1), time_by = "day")
time_seq(today, length.out = 10, time_by = "day")
time_seq(today, length.out = 10, time_by = "hour")
time_seq(today, today + years(1), time_by = list("days" = 1)) # Alternative
time_seq(today, today + years(1), time_by = "week")
time_seq(today, today + years(1), time_by = "fortnight")
time_seq(today, today + years(1), time_by = "year")
time_seq(today, today + years(10), time_by = "year")
time_seq(today, today + years(100), time_by = "decade")
# Datetimes
time_seq(now, now + years(1), time_by = "12 hours")
time_seq(now, now + years(1), time_by = "day")
time_seq(now, now + years(1), time_by = "week")
time_seq(now, now + years(1), time_by = "fortnight")
time_seq(now, now + years(1), time_by = "year")
time_seq(now, now + years(10), time_by = "year")
time_seq(now, today + years(100), time_by = "decade")
# You can seamlessly mix dates and datetimes with no errors.
time_seq(now, today + days(3), time_by = "day")
time_seq(now, today + days(3), time_by = "hour")
time_seq(today, now + days(3), time_by = "day")
time_seq(today, now + days(3), time_by = "hour")
# Choose between durations or periods
start <- dmy(31012020)
# If time_type is left as is,
# periods are used for days, weeks, months and years.
time_seq(start, time_by = "month", length.out = 12,
time_type = "period")
time_seq(start, time_by = "month", length.out = 12,
time_type = "duration")
# Notice how strange base R version is.
seq(start, by = "month", length.out = 12)
# Roll forward or backward impossible dates
leap <- dmy(29022020) # Leap day
end <- dmy(01032021)
# 3 different options
time_seq(leap, to = end, time_by = "year",
roll_month = "NA")
time_seq(leap, to = end, time_by = "year",
roll_month = "postday")
time_seq(leap, to = end, time_by = "year",
roll_month = getOption("timeplyr.roll_month", "preday"))