‘squarebrackets’ provides the long_x and long_set methods
to perform sub-set operations on the interior of a vector,
without any indexing vector at all.
The advantage of not using an indexing vector for sub-set operations on a long vector,
is that it may safe the user a large amount of memory.
Instead of an indexing vector, they use the stride argument.
The following can be used in the stride argument:
a stride_pv object:
Use thisstridetype to specify subsets based on property values, likep == v, wherepis an atomic vector of properties (likenames(x)), andvis a value (or range of values)pmight contain.
stride_pv does not actually allocate an indexing vector.a stride_seq object:
Use thisstridetype to specify a sequence in the form ofseq(from, to, by), without actually allocating a sequence indexing vector.a stride_ptrn object:
Use thisstridetype to specify a patterned sequence in the form of(start:end)[pattern],
wherestartandendare natural scalars andpatternis a logical vector.
stride_ptrn specifies this sequence without actually allocating an indexing vector.A formula in the form of
~ from:to:by, wherefrom,toandbyare natural scalars.
This will be interpreted asstride_seq(from, to, by).A formula in the form of
~ start:end:ptrn, wherestartandendare natural scalars, andptrnis a logical vector.
This will be interpreted asstride_ptrn(start, end, ptrn).
In both formula forms, the .N keyword is available,
which equals length(x)
(where x is the input vector).
For example, ~ 2:(.N - 1):c(TRUE, FALSE, FALSE, TRUE)
is equivalent to ~ 2:(length(x) - 1):c(TRUE, FALSE, FALSE, TRUE),
and will be interpreted as stride_ptrn(2, length(x) - 1, c(TRUE, FALSE, FALSE, TRUE)).
Examples
# extract all elements of x with the name "a":
nms <- c(letters, LETTERS, month.abb, month.name) |> rep_len(1e6)
x <- mutatomic(1:1e6, names = nms)
head(x)
#> a b c d e f
#> 1 2 3 4 5 6
#> mutatomic
#> typeof: integer
stride <- stride_pv(names(x), v = "a")
long_x(x, stride) |> head()
#> a a a a a a
#> 1 77 153 229 305 381
#> mutatomic
#> typeof: integer
# find all x smaller than or equal to 5, and replace with `-1000`:
stride <- stride_pv(x, v = c(-Inf, 5))
long_set(x, stride, rp = -1000L)
head(x, n = 10)
#> a b c d e f g h i j
#> -1000 -1000 -1000 -1000 -1000 6 7 8 9 10
#> mutatomic
#> typeof: integer
x <- mutatomic(1:1e7)
# extract elements 2 to 9
long_x(x, ~ 2:9:1)
#> [1] 2 3 4 5 6 7 8 9
#> mutatomic
#> typeof: integer
# reverse:
long_x(x, ~ 9:2:1)
#> [1] 9 8 7 6 5 4 3 2
#> mutatomic
#> typeof: integer
# remove:
long_x(x, ~ 1:(.N - 10):1, -1) # all elements except the last 10
#> [1] 9999991 9999992 9999993 9999994 9999995 9999996 9999997 9999998
#> [9] 9999999 10000000
#> mutatomic
#> typeof: integer
# replace every other element:
x <- mutatomic(1:1e7)
long_set(x, ~ 2:.N:2, rp = -1)
#> coercing replacement to integer
head(x)
#> [1] 1 -1 3 -1 5 -1
#> mutatomic
#> typeof: integer
# replace all elements except the first element:
x <- mutatomic(1:1e7)
long_set(x, ~1:1:1, use = -1, rp = -1)
#> coercing replacement to integer
head(x)
#> [1] 1 -1 -1 -1 -1 -1
#> mutatomic
#> typeof: integer
# extract pattern c(TRUE, FALSE, FALSE, TRUE) from first 20 elements:
x <- mutatomic(1:1e7)
long_x(x, ~ 1:20:c(TRUE, FALSE, FALSE, TRUE))
#> [1] 1 4 5 8 9 12 13 16 17 20
#> mutatomic
#> typeof: integer