rray-reduce: Reduce an array along axes

rray-reduceR Documentation

Reduce an array along axes

Description

  • rray_sum() computes the sum along the specified axes.

  • rray_prod() computes the product along the specified axes.

  • rray_mean() computes the mean along the specified axes.

  • rray_all() checks if all values are TRUE along the specified axes.

  • rray_any() checks if any value is TRUE along the specified axes.

  • rray_max() computes the maximum along the specified axes.

  • rray_min() computes the minimum along the specified axes.

Usage

rray_sum(x, ..., axes, na_rm = FALSE)

rray_prod(x, ..., axes, na_rm = FALSE)

rray_mean(x, ..., axes, na_rm = FALSE)

rray_all(x, ..., axes, na_rm = FALSE)

rray_any(x, ..., axes, na_rm = FALSE)

rray_max(x, ..., axes, na_rm = FALSE)

rray_min(x, ..., axes, na_rm = FALSE)

Arguments

x

An array.

...

These dots are for future extensions and must be empty.

axes

The axes to reduce along, given by position or by title.

na_rm

If TRUE, missing values are removed before reducing.

Details

The dimensionality of x is retained in the result, with the reduced axes collapsed to size 1.

Value

An array with the same dimensionality as x, but with the dimensions along axes reduced to 1.

Sum

Logicals are summed as integers. If an integer sum doesn't fit in an integer, an error is thrown. Only the final sum is checked, and NA wins over an overflow:

x <- c(.Machine$integer.max, 1L, -1L)
rray_sum(x, axes = 1L) # .Machine$integer.max

x <- c(.Machine$integer.max, 1L, NA)
rray_sum(x, axes = 1L) # NA

Product

Logicals and integers are cast to double.

When NA and NaN are both present, you get one of them, but which one depends on the platform and the order of the values. Either way, is.na() is TRUE. Integers can hit this too, since a product that overflows to Inf becomes NaN when multiplied by 0:

rray_prod(c(NA, NaN), axes = 1L) # NA or NaN

x <- c(rep(.Machine$integer.max, 40L), 0L, NA)
rray_prod(x, axes = 1L) # NA or NaN

Mean

Logicals and integers are cast to double.

When NA and NaN are both present, you get one of them, but which one depends on the platform and the order of the values, like mean(). Either way, is.na() is TRUE:

rray_mean(c(NA, NaN), axes = 1L) # NA or NaN
rray_mean(c(NaN, NA), axes = 1L) # NA or NaN

Min / Max

rray_max() and rray_min() keep the type of x. With nothing to reduce, such as an axis of dimension 0, rray_max() returns the smallest value of that type and rray_min() returns the largest:

type rray_max() rray_min()
logical FALSE TRUE
integer -.Machine$integer.max .Machine$integer.max
double -Inf Inf

When NA and NaN are both present, NA wins, like max() and min():

rray_max(c(NA, NaN), axes = 1L) # NA
rray_max(c(NaN, NA), axes = 1L) # NA

Examples

x <- array(1:10, c(5L, 2L))

# Sum along rows
rray_sum(x, axes = 1L)

# Sum along columns
rray_sum(x, axes = 2L)

# Sum along both axes
rray_sum(x, axes = c(1L, 2L))

# Sum along rows, by title
y <- rray_set_titles(x, c("row", "col"))
rray_sum(y, axes = "row")

# Product along rows
rray_prod(x, axes = 1L)

# Mean along rows
rray_mean(x, axes = 1L)

y <- array(c(TRUE, TRUE, FALSE, TRUE), c(2L, 2L))

rray_all(y, axes = 1L)
rray_any(y, axes = 1L)

# Maximum and minimum along columns
rray_max(x, axes = 2L)
rray_min(x, axes = 2L)

rray documentation built on Nov. 17, 2026, 5:07 p.m.