rray_subset: Get or set dimensions of an array

Description Usage Arguments Details Value Differences from base R See Also Examples

View source: R/subset.R

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

 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]

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.

Details

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

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

See Also

pad()

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

Examples

 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

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