| Title: | Compact Vector Replication |
|---|---|
| Description: | Replicates vectors using ALTREP (Alternative Representations for R Objects), avoiding unnecessary memory allocation. When a vector is repeated many times, only a reference to the original data is stored rather than copying the full expanded replicates into memory. The expanded data is only materialised if it is modified, making repeated vectors cheap to create and pass around. This is particularly useful when working with large repeated sequences, such as replicated index vectors, simulation inputs, or repeated reference values in data pipelines. |
| Authors: | Mitchell O'Hara-Wild [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-6729-7695>), Gabriel Becker [ctb] (For developing the example ALTREP package vectorwindow that was foundational to this package.) |
| Maintainer: | Mitchell O'Hara-Wild <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-06-19 13:36:18 UTC |
| Source: | https://github.com/mitchelloharawild/vecrep |
Creates a repeated vector backed by an ALTREP representation, avoiding materialisation of the full vector in memory until necessary.
rep_altrep(x, times = 1L, each = 1L)rep_altrep(x, times = 1L, each = 1L)
x |
A vector to repeat. Must be one of: double, integer, logical, complex, raw, character, or list (including classed variants thereof). |
times |
A single positive integer giving the number of times to repeat
the whole (each-expanded) pattern. Defaults to |
each |
A single positive integer giving the number of times each
element is repeated before moving to the next. Defaults to |
Supported types: double, integer, logical, complex, raw, character, and list.
Classed vectors (e.g. factor, Date, POSIXct) are handled
transparently: the class attribute is preserved on the returned object so
S3 dispatch continues to work without forcing materialisation.
times and each can be combined freely, matching the behaviour of
base::rep(): each replicates individual elements first, then times
repeats the resulting pattern. Providing only times is equivalent to
rep(x, times = times); providing only each is equivalent to
rep(x, each = each).
An ALTREP vector of the same type and class as x, with length
length(x) * each * times.
rep_altrep(letters[1:4], times = 2) rep_altrep(letters[1:4], each = 2) rep_altrep(letters[1:4], times = 2, each = 3) rep_altrep(1L:4L, each = 2L) rep_altrep(c(TRUE, FALSE, NA), each = 2L, times = 3L) rep_altrep(factor(c("a", "b", "c")), each = 2L) rep_altrep(as.Date("2024-01-01") + 0:2, each = 2L) rep_altrep(c("foo", "bar"), times = 5L) rep_altrep(list(1, "a", TRUE), each = 2L, times = 2L)rep_altrep(letters[1:4], times = 2) rep_altrep(letters[1:4], each = 2) rep_altrep(letters[1:4], times = 2, each = 3) rep_altrep(1L:4L, each = 2L) rep_altrep(c(TRUE, FALSE, NA), each = 2L, times = 3L) rep_altrep(factor(c("a", "b", "c")), each = 2L) rep_altrep(as.Date("2024-01-01") + 0:2, each = 2L) rep_altrep(c("foo", "bar"), times = 5L) rep_altrep(list(1, "a", TRUE), each = 2L, times = 2L)