sequence2 {timeplyr}R Documentation

Utilities for creating useful sequences

Description

sequence2 is an extension to sequence which accepts decimal number increments.
seq_id can be paired with sequence2 to group individual sequences.
seq_v is a vectorised version of seq.
window_sequence creates a vector of window sizes for rolling calculations.
lag_sequence creates a vector of lags for rolling calculations.
lead_sequence creates a vector of leads for rolling calculations.

Usage

sequence2(size, from = 1L, by = 1L)

seq_id(size)

seq_v(from = 1L, to = 1L, by = 1L)

seq_size(from, to, by = 1L)

window_sequence(size, k, partial = TRUE, ascending = TRUE)

lag_sequence(size, k, partial = TRUE)

lead_sequence(size, k, partial = TRUE)

Arguments

size

Vector of sequence lengths.

from

Start of sequence(s).

by

Unit increment of sequence(s).

to

End of sequence(s).

k

Window/lag size.

partial

Should partial windows/lags be returned? Default is TRUE.

ascending

Should window sequence be ascending? Default is TRUE.

Details

sequence2() works in the same way as sequence() but can accept non-integer by values. It also recycles from and to, in the same way as sequence().
If any of the sequences contain values > .Machine$integer.max, then the result will always be a double vector.

from can be also be a date, date-time, or any object that supports addition and multiplication.

seq_v() is a vectorised version of seq() that strictly accepts only the arguments from, to and by.

Value

A vector of length sum(size) except for seq_v which returns a vector of size sum((to - from) / (by + 1))

Examples

library(timeplyr)

sequence(1:3)
sequence2(1:3)

sequence(1:3, by = 0.1)
sequence2(1:3, by = 0.1)

sequence(c(3, 2), by = c(-0.1, 0.1))
sequence2(c(3, 2), by = c(-0.1, 0.1))

# We can group sequences using seq_id
size <- c(7, 0, 3)
from <- 1
by <- c(-0.1, 0.1, 1/3)
x <- sequence2(size, from, by)
names(x) <- seq_id(size)
x

# Vectorised version of seq()
seq_v(1, 10, by = c(1, 0.5))
# Same as below
c(seq(1, 10, 1), seq(1, 10, 0.5))

# Programmers may use seq_size() to determine final sequence lengths

sizes <- seq_size(1, 10, by = c(1, 0.5))
print(paste(c("sequence sizes: (", sizes, ") total size:", sum(sizes)),
            collapse = " "))

# We can group sequences using seq_id

from <- Sys.Date()
to <- from + 10
by <- c(1, 2, 3)
x <- seq_v(from, to, by)
names(x) <- seq_id(seq_size(from, to, by))
x

# Utilities for rolling calculations

window_sequence(c(3, 5), 3)
window_sequence(c(3, 5), 3, partial = FALSE)
window_sequence(c(3, 5), 3, partial = TRUE, ascending = FALSE)
# One can for example use these in data.table::frollsum


[Package timeplyr version 0.5.0 Index]