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.

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

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)
#> [1] 1 3 5 7 9 11 13 15
# Extract the first row and first two columns rray_extract(x, 1, 1:2)
#> [1] 1 3 9 11
# You can assign directly to these elements rray_extract(x, 1, 1:2) <- NA x
#> <rray<int>[,4,2][2]> #> , , 1 #> #> [,1] [,2] [,3] [,4] #> r1 NA NA 5 7 #> r2 2 4 6 8 #> #> , , 2 #> #> [,1] [,2] [,3] [,4] #> r1 NA NA 13 15 #> r2 10 12 14 16 #>