The slicev_
- methods are similar to the i_
/ss_
- methods,
except they don't require an indexing vector,
and are designed for memory efficiency. counv(y, v, from, to)
counts how often a value, or range of values, v
,
occurs in a vector subset y[from:to]
.
Usage
slicev_x(x, ...)
# Default S3 method
slicev_x(
x,
...,
y = x,
v = NULL,
na = FALSE,
r = TRUE,
from = NULL,
to = NULL,
use.names = TRUE,
sticky = getOption("squarebrackets.sticky", FALSE)
)
slicev_set(x, ...)
# Default S3 method
slicev_set(
x,
...,
y = x,
v = NULL,
na = FALSE,
r = TRUE,
from = NULL,
to = NULL,
rp,
tf
)
countv(y, ..., v = NULL, na = FALSE, r = TRUE, from = NULL, to = NULL)
Arguments
Value
Similar to the i_
/ss_
methods.
For countv()
: A single number,
giving the number of elements matching the specified condition.
Examples
# basic idea ====
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
# memory efficient form of sum(x <= 10):
countv(x, v = c(-Inf, 10))
#> [1] 10
# extract all elements of x with the name "a":
slicev_x(x, y = names(x), v = "a") |> 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`:
slicev_set(x, y = x, v = c(-Inf, 5), 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
################################################################################
# Numeric range ====
#
x <- mutatomic(1:1e6)
head(x)
#> [1] 1 2 3 4 5 6
#> mutatomic
#> typeof: integer
slicev_x(x, v= c(-Inf, 5)) # x[x <= 5]
#> [1] 1 2 3 4 5
#> mutatomic
#> typeof: integer
################################################################################
# Character ====
#
x <- stringi::stri_rand_shuffle(rep("hello", 1e5))
head(x)
#> [1] "ehllo" "olelh" "olhle" "elohl" "olhel" "lleoh"
slicev_x(x, v = "hello") |> head() # find "hello"
#> [1] "hello" "hello" "hello" "hello" "hello" "hello"
# find 2 possible misspellings of "hello":
slicev_x(x, v = c("holle", "helol")) |> head()
#> [1] "helol" "helol" "holle" "holle" "holle" "helol"