| rray_slice | R Documentation |
rray_slice() selects positions along each axis of x. The result always
has the same dimensionality as x.
rray_slice_axis() is rray_slice() restricted to a single axis, which can
be more ergonomic to write. rray_slice_rows() and rray_slice_columns()
are shortcuts for axis = 1 and axis = 2.
The _assign versions replace the selected positions with value and
return a modified copy of x.
rray_slice(x, ...)
rray_slice_axis(x, i, ..., axis)
rray_slice_rows(x, i)
rray_slice_columns(x, i)
rray_slice_assign(x, ..., value)
rray_slice_assign_axis(x, i, ..., axis, value)
rray_slice_assign_rows(x, i, value)
rray_slice_assign_columns(x, i, value)
x |
An array. |
... |
One unnamed subscript for each axis of
For |
i |
A subscript for the selected axis. It takes any of the forms
allowed in |
axis |
The axis to slice along, given by position or by title. |
value |
An array to assign to the selected positions. It is cast to
the type of |
NA in a subscript gives missing values in the result. For raw arrays the
missing value is as.raw(0), and for list arrays it is NULL. If the axis
has names, the name of a missing value is "". For assignment, NA in a
subscript leaves x unchanged at that position but uses one element of
value.
Unlike [, an empty argument does not select a whole axis. Use TRUE
instead, so that every axis is provided explicitly:
x <- array(1:24, c(2L, 3L, 4L)) # Base R x[1, , , drop = FALSE] # rray rray_slice(x, 1, TRUE, TRUE) # Or, more simply rray_slice_axis(x, 1, axis = 1) rray_slice_rows(x, 1)
rray_slice(), rray_slice_axis(), rray_slice_rows(), and
rray_slice_columns() return an array with the same type and
dimensionality as x. The dimension of each axis is the number of
positions selected on it. The names of each axis are selected along with
the values. The titles of x are kept.
The _assign versions return x with the selected positions replaced.
The type, dimensions, and names of x are kept.
x <- array(1:24, c(2L, 3L, 4L))
# The first row
rray_slice(x, 1, TRUE, TRUE)
# Reorder and repeat positions
rray_slice(x, c(2, 1, 2), 1, 4)
# Drop positions with negative locations
rray_slice(x, TRUE, -2, -(1:2))
# Select by name
y <- array(1:6, c(2L, 3L), dimnames = list(c("a", "b"), c("c", "d", "e")))
rray_slice(y, "b", c("e", "c"))
# Subscripts can be spliced into `...`
subscripts <- rep(list(TRUE), rray_dimensionality(x))
subscripts[[3]] <- c(4, 1)
rray_slice(x, !!!subscripts)
# Slice a single axis
rray_slice_axis(x, c(4, 1), axis = 3)
rray_slice_rows(y, c("b", "a"))
rray_slice_columns(x, -2)
# Slice a single axis, by title
z <- rray_set_titles(y, c("row", "col"))
rray_slice_axis(z, c("e", "c"), axis = "col")
# Assign one value to every first row
rray_slice_assign(x, 1, TRUE, TRUE, value = 0L)
rray_slice_assign_rows(x, 1, 0L)
# Or broadcast `value` to the dimensions of the selection
value <- array(c(100L, 200L), c(1L, 2L))
rray_slice_assign(x, TRUE, c(1, 3), 1, value = value)
value <- array(c(100L, 200L), c(2L, 1L))
rray_slice_assign_axis(x, 3, axis = 2, value = value)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.