slice: Create a Reference Slice to a Vector

Description Usage Arguments Details Value Examples

View source: R/slice.R

Description

Create a reference to a 'part' of an R object. Use deref or `!` to obtain the values within the referenced object.

Usage

1
slice(x, ...)

Arguments

x

object to be referenced; must be a symbol or character

...

objects passed to x[...] when dereferenced

Details

slice is similar to ref; it creates a reference to another R object. There are two main differences with ref. First, slice only accepts names or characters instead of expressions. Second, slice records a part of the underlying object. slice(x, 1:2, 3) is equivalent to the reference of x[1:2, 3]. This is similar to ref(x[1:2, 3]), though the implementation is different. ref would create an expression with a reference to x, while slice(x, 1:2, 3) creates a list with a reference to x and the extract inputs. slice is more efficient, but is limited in its capabilities.

Value

object of class "slice" and "ref"

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## Vector Slice
x <- 10:1

slice_x <- slice(x, 2:4)
identical(!slice_x, 9:7)   # TRUE

x <- x - 2
identical(!slice_x, 7:5)   # TRUE

## Matrix Slice
y <- matrix(1:9, nrow=3)
slice_y <- slice(y, 2, 3)

identical(!slice_y, y[2, 3])   # TRUE

refer documentation built on Nov. 8, 2021, 5:08 p.m.