sequence_ {cheapr} | R Documentation |
Utilities for creating many sequences
Description
sequence_
is an extension to sequence which
accepts decimal number increments.
seq_id
can be paired with sequence_
to group individual sequences.
seq_
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
sequence_(size, from = 1L, by = 1L, add_id = FALSE)
seq_id(size)
seq_(from = 1L, to = 1L, by = 1L, add_id = FALSE)
seq_size(from, to, by = 1L)
window_sequence(size, k, partial = TRUE, ascending = TRUE, add_id = FALSE)
lag_sequence(size, k, partial = TRUE, add_id = FALSE)
lead_sequence(size, k, partial = TRUE, add_id = FALSE)
Arguments
size |
Vector of sequence lengths. |
from |
Start of sequence(s). |
by |
Unit increment of sequence(s). |
add_id |
Should the ID numbers of the sequences be added as names?
Default is |
to |
End of sequence(s). |
k |
Window/lag size. |
partial |
Should partial windows/lags be returned? Default is |
ascending |
Should window sequence be ascending? Default is |
Details
sequence_()
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_()
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_
which
returns a vector of size sum((to - from) / (by + 1))
Examples
library(cheapr)
sequence(1:3)
sequence_(1:3)
sequence(1:3, by = 0.1)
sequence_(1:3, by = 0.1)
# Add IDs to the sequences
sequence_(1:3, by = 0.1, add_id = TRUE)
# Turn this quickly into a data frame
enframe_(sequence_(1:3, by = 0.1, add_id = TRUE))
sequence(c(3, 2), by = c(-0.1, 0.1))
sequence_(c(3, 2), by = c(-0.1, 0.1))
# Vectorised version of seq()
seq_(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_(from, to, by, add_id = TRUE)
class(x) <- "Date"
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