Learn R Programming

rray (version 0.0.0.9000)

rray_yank: Get or set elements of an array by position

Description

rray_yank() is the counterpart to rray_extract(). It extracts elements from an array by position. It always drops dimensions (unlike rray_subset()), and a 1D vector is always returned. It powers the [[ method for rrays.

Usage

rray_yank(x, i)

# S3 method for vctrs_rray [[(x, i, ...)

rray_yank(x, i) <- value

# S3 method for vctrs_rray [[(x, i, ...) <- value

Arguments

x

A vector, matrix, array or rray.

i

One of the following:

  • An integer vector specifying the positions of the elements to yank.

  • A 1D logical vector of length 1, or rray_elems(x).

  • A logical with the same dimension as x.

...

Not used. An error is thrown if extra arguments are supplied here.

value

A 1D value to be assigned to the location yanked by i. It will be cast to the type and length of x after being yanked by i.

Details

Dimension names are only kept in the special case of calling rray_yank() on a 1D object. Otherwise, the method of keeping them is not well defined.

rray_yank() works with base R objects.

rray_yank() is meant as a replacement for the traditional behavior of x[i] since [ for rray objects is much stricter. Separating this special behavior into a different function is less surprising.

Additionally, base R has x[[i]] which restricts i to be length 1. For rray objects, [[ acts more like x[i], always dropping to 1D, but allowing for the selection of multiple positions.

You cannot do x[[i, j, ...]] with rrays. For that behavior, see rray_extract().

See Also

Other rray subsetters: rray_extract, rray_slice, rray_subset<-

Examples

Run this code
# NOT RUN {
x <- rray(10:17, c(2, 2, 2))

# Resulting dimension is always 1D
rray_yank(x, 1:3)

# With logical
rray_yank(x, FALSE)
rray_yank(x, rep(c(TRUE, FALSE), times = rray_elems(x) / 2))

# You can assign a 1D vector to these yanked selections
rray_yank(x, c(1, 3, 5)) <- 9

# Logicals with the same dim as `x`
# can also be used as a yank indexer
lgl <- rray(c(TRUE, FALSE), c(2, 2, 2))
rray_yank(x, lgl)

# And you can set elements in these locations
rray_yank(x, lgl) <- NA

# `[[` for rray objects is powered by
# rray_yank().
# This can be very useful for
# performing assignment
# by position.
x[[c(1, 3)]] <- NA

# Logical arrays with the same shape as `x`
# can be assigned to. This is a useful way
# to get rid of NA values.
idx <- array(is.na(as.vector(x)), c(2, 2, 2))

x[[idx]] <- 0

# }

Run the code above in your browser using DataLab