| Title: | Compact Vector Replication |
| Version: | 0.1.0 |
| 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. |
| Depends: | R (≥ 3.5) |
| License: | MIT + file LICENSE |
| URL: | https://pkg.mitchelloharawild.com/vecrep/, https://github.com/mitchelloharawild/vecrep |
| BugReports: | https://github.com/mitchelloharawild/vecrep/issues |
| Encoding: | UTF-8 |
| Language: | en-GB |
| ByteCompile: | yes |
| Suggests: | spelling, testthat (≥ 3.0.0) |
| Config/testthat/edition: | 3 |
| Config/roxygen2/version: | 8.0.0 |
| NeedsCompilation: | yes |
| Packaged: | 2026-06-11 16:21:26 UTC; mitchell |
| Author: | Mitchell O'Hara-Wild
|
| Maintainer: | Mitchell O'Hara-Wild <mail@mitchelloharawild.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-06-18 16:50:02 UTC |
vecrep: Replication of Numeric Vectors Via ALTREP
Description
Replicates numeric vectors using R's ALTREP framework, avoiding unnecessary memory allocation. When a numeric 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.
Author(s)
Maintainer: Mitchell O'Hara-Wild mail@mitchelloharawild.com (ORCID)
Authors:
Mitchell O'Hara-Wild mail@mitchelloharawild.com (ORCID)
Other contributors:
Gabriel Becker (For developing the example ALTREP package vectorwindow that was foundational to this package.) [contributor]
See Also
Useful links:
Report bugs at https://github.com/mitchelloharawild/vecrep/issues
Repeat a vector using ALTREP
Description
Creates a repeated vector backed by an ALTREP representation, avoiding materialisation of the full vector in memory until necessary.
Usage
rep_altrep(x, times = 1L, each = 1L)
Arguments
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 |
Details
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).
Value
An ALTREP vector of the same type and class as x, with length
length(x) * each * times.
Examples
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)