Learn R Programming

rray (version 0.1.0)

rray_subset<-: Get or set dimensions of an array

Description

rray_subset() extracts dimensions from an array by index. It powers [ for rray objects. Notably, it never drops dimensions, and ignores trailing commas.

Usage

rray_subset(x, ...) <- value

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

rray_subset_assign(x, ..., value)

rray_subset(x, ...)

# S3 method for vctrs_rray [(x, ..., drop = FALSE)

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

drop

Ignored, but preserved for better error messages with code that might have used arrays before.

Value

x subset by the specification defined in the ....

The assignment variants return x modified by having the elements of value inserted into the positions defined by ....

Differences from base R

  • rray_subset() never drops dimensions.

  • rray_subset() ignores trailing commas. This has the nice property of making x[1] == x[1,].

  • rray_subset()<- casts value to x, rather than casting x to value.

Details

rray_subset() and its assignment variant can also be used with base R matrices and arrays to get rray subsetting behavior with them.

See Also

pad()

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

Examples

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

# `rray_subset()` powers `[` so these are identical
rray_subset(x, 1)
x[1]

# Trailing dots are ignored, so these are identical
x[1]
x[1,]

# Missing arguments are treated as selecting the
# entire dimension, consistent with base R.
# This selects all of the rows, and the first column.
x[,1]

# Notice that you can't actually do the above with base
# R. It requires you to fully specify the dimensions of `x`.
# This would throw an error.
x_arr <- as_array(x)
try(x_arr[,1])

# To get the same behavior, you have to do:
x_arr[, 1, , drop = FALSE]

# Note that you can use base R arrays with `rray_subset()`
rray_subset(x_arr, , 1)

# For higher dimensional objects, `pad()` can be
# useful for automatically adding commas. The
# following are equivalent:
x[pad(), 1]
x[, , 1]

# You can assign to index locations with
# `x[...] <- value`
# This assigns 99 to the entire first row
x[1] <- 99
x

# First row in the first
# element of the 3rd dimension
x[1, , 1] <- 100
x

# Note that `value` is broadcast to the shape
# of `x[...]`. So this...
x[,1] <- matrix(5)

# ...becomes the same as
x[,1] <- array(5, c(2, 1, 2))

# You can also use `rray_subset<-()` directly to
# use these semantics with base R
rray_subset(x_arr, , 1) <- matrix(5)
x_arr

# }

Run the code above in your browser using DataLab