Description Usage Arguments Details Value Differences from base R See Also Examples
rray_subset()
extracts dimensions from an array by index. It powers [
for rray objects. Notably, it never drops dimensions, and ignores
trailing commas.
1 2 3 4 5 6 7 8 9 10 11 | rray_subset(x, ...) <- value
## S3 replacement method for class 'vctrs_rray'
x[...] <- value
rray_subset_assign(x, ..., value)
rray_subset(x, ...)
## S3 method for class 'vctrs_rray'
x[..., drop = FALSE]
|
x |
A vector, matrix, array, or rray. |
... |
A specification of indices to extract.
|
value |
The value to assign to the location specified by |
drop |
Ignored, but preserved for better error messages with code that might have used arrays before. |
rray_subset()
and its assignment variant can also be used with base R
matrices and arrays to get rray subsetting behavior with them.
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 ...
.
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
.
Other rray subsetters: rray_extract<-
,
rray_slice<-
, rray_yank<-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | 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
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.