rray_extract: Get or set elements of an array by index

Description Usage Arguments Details Value See Also Examples

View source: R/extract.R

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

1
2
3
4
5

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 ....

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.

Value

A 1D vector of elements extracted from x.

See Also

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

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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

rray documentation built on July 23, 2019, 5:04 p.m.