Learn R Programming

rray (version 0.1.0)

rray_extract<-: Get or set elements of an array by index

Description

rray_extract() is the counterpart to rray_yank(). It extracts elements from an array by index. It always drops dimensions (unlike rray_subset()), and a 1D object is always returned.

Usage

rray_extract(x, ...) <- value

rray_extract_assign(x, ..., value)

rray_extract(x, ...)

Arguments

x

A vector, matrix, array, or rray.

...

A specification of indices to extract.

  • Integer-ish indices extract specific elements of dimensions.

  • Logical indices must be length 1, or the length of the dimension you are subsetting over.

  • Character indices are only allowed if x has names for the corresponding dimension.

  • NULL is treated as 0.

value

The value to assign to the location specified by .... Before assignment, value is cast to the type and dimension of x after extracting elements with ....

Value

A 1D vector of elements extracted from x.

Details

Like [[, rray_extract() will never keep dimension names.

rray_extract() works with base R objects as well.

rray_extract() is similar to the traditional behavior of x[[i, j, ...]], but allows each subscript to have length >1.

See Also

Other rray subsetters: rray_slice<-, rray_subset<-, rray_yank<-

Examples

Run this code
# NOT RUN {
x <- rray(1:16, c(2, 4, 2), dim_names = list(c("r1", "r2"), NULL, NULL))

# Extract the first row and flatten it
rray_extract(x, 1)

# Extract the first row and first two columns
rray_extract(x, 1, 1:2)

# You can assign directly to these elements
rray_extract(x, 1, 1:2) <- NA
x

# }

Run the code above in your browser using DataLab