Skip to contents

The slice_ - methods are similar to the sb_ - methods, except they don't require an indexing vector, and are designed for memory efficiency.

Usage

slice_x(x, ...)

# Default S3 method
slice_x(
  x,
  from = NULL,
  to = NULL,
  by = 1L,
  ...,
  use.names = TRUE,
  sticky = getOption("squarebrackets.sticky", FALSE)
)

slice_wo(x, ...)

# Default S3 method
slice_wo(
  x,
  from = NULL,
  to = NULL,
  by = 1L,
  ...,
  use.names = TRUE,
  sticky = getOption("squarebrackets.sticky", FALSE)
)

slice_set(x, ...)

# Default S3 method
slice_set(x, from = NULL, to = NULL, by = 1L, inv = FALSE, ..., rp, tf)

Arguments

x

an atomic object.
For slice_set it must be a mutable_atomic variable.

...

see squarebrackets_method_dispatch.

from, to, by

see cp_seq.

use.names

Boolean, indicating if flat names should be preserved.
Note that, since the slice_ methods operates on flat indices only, dimensions and dimnames are always dropped.

sticky

see squarebrackets_options.

inv

Boolean, indicating whether to invert the sequence.
If TRUE, slice_set() will apply replacement/transformation on all elements of the vector, except for the elements of the specified sequence.

rp, tf

see squarebrackets_modify.

Value

Similar to the sb_ methods.

Examples



x <- mutable_atomic(1:1e7)

# extract:
slice_x(x, 1, 10)
#>  [1]  1  2  3  4  5  6  7  8  9 10
#> mutable_atomic 
#> typeof:  integer 

# reverse:
slice_x(x, -1i, 1) |> head()
#> [1] 10000000  9999999  9999998  9999997  9999996  9999995
#> mutable_atomic 
#> typeof:  integer 

# remove:
slice_wo(x, 1, -11i) # all elements except the last 10
#>  [1]  9999991  9999992  9999993  9999994  9999995  9999996  9999997  9999998
#>  [9]  9999999 10000000
#> mutable_atomic 
#> typeof:  integer 

# replace every other element:
x <- mutable_atomic(1:1e7)
slice_set(x, 2, -1i, 2, rp = -1)
#> coercing replacement to integer
head(x)
#> [1]  1 -1  3 -1  5 -1
#> mutable_atomic 
#> typeof:  integer 

# replace all elements except the first element:
x <- mutable_atomic(1:1e7)
slice_set(x, 1, 1, inv = TRUE, rp = -1)
#> coercing replacement to integer
head(x)
#> [1]  1 -1 -1 -1 -1 -1
#> mutable_atomic 
#> typeof:  integer