Learn R Programming

rray (version 0.0.0.9000)

rray_slice: Get or set a slice of an array

Description

rray_slice() is a shortcut wrapper around rray_subset() that is useful for easily subsetting a single axis.

Usage

rray_slice(x, i, axis)

rray_slice(x, i, axis) <- value

rray_slice_assign(x, i, axis, value)

Arguments

x

A vector, matrix, array or rray.

i

Indices to extract along a single axis.

  • 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.

axis

An integer. The axis to subset.

value

A value to be assigned to the slice of x subset by i and axis. It will be cast to the type and dimension of the slice of x.

Details

rray_slice() can be used with base R objects as well as rrays.

See Also

Other rray subsetters: rray_extract, rray_subset<-, rray_yank

Examples

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

# Selecting the first column
rray_slice(x, i = 1, axis = 2)

# rray_slice() is particularly useful for
# subsetting higher dimensions because you don't
# have to worry about the commas
rray_slice(x, i = 2, axis = 4)

# Compare the above with the equivalent
# using `[`
x[, , , 2]

# `i` can be a character vector if `x` has names along `axis`
x <- rray_set_axis_names(x, axis = 4, c("foo", "bar"))
rray_slice(x, "bar", axis = 4)

# The assignment variation can be useful
# for assigning to higher dimensional elements
rray_slice(x, 1, 3) <- matrix(c(99, 100), nrow = 1)

# }

Run the code above in your browser using DataLab