Learn R Programming

⚠️There's a newer version (0.1.0) of this package.Take me there.

rray

Introduction

rray (said: “r-ray”) is an array manipulation library for R. It has three main goals:

  1. To provide an rray class that tries to be stricter and more consistent than base R arrays, similar in spirit to tibble.

  2. To support broadcasting throughout the package, which allows for novel yet intuitive array operations that have been missing from the R ecosystem.

  3. To provide a consistent, powerful toolkit for array based manipulation, usable by both the new rray objects and base R matrices/arrays.

View the vignettes for each goal on the website to learn more about how to use rray.

  • vignette("the-rray")
  • vignette("broadcasting")
  • vignette("toolkit")
library(rray)

What can it do?

In short, rray tries to make array manipulation in R more intuitive by combining the idea of broadcasting with knowing when to not drop dimensions. This results in operations such as:

x <- rray(1:6, dim = c(3, 2))

# Compute proportions along the 1st dimension
x / rray_sum(x, axes = 1)
#> <rray<dbl>[,2][3]>
#>           [,1]      [,2]
#> [1,] 0.1666667 0.2666667
#> [2,] 0.3333333 0.3333333
#> [3,] 0.5000000 0.4000000

# Equivalent base R syntax
sweep(x, 2, apply(x, 2, sum), "/")
#> <rray<dbl>[,2][3]>
#>           [,1]      [,2]
#> [1,] 0.1666667 0.2666667
#> [2,] 0.3333333 0.3333333
#> [3,] 0.5000000 0.4000000

These concepts are baked into every part of rray, and show up in other functions such as rray_bind(). Using broadcasting, rray_bind() can bind arrays together in ways that base R cannot with the native cbind() and rbind() functions.

a <- array(c(1, 2), dim = c(2, 1))
b <- array(c(3, 4), dim = c(1, 2))

a
#>      [,1]
#> [1,]    1
#> [2,]    2

b
#>      [,1] [,2]
#> [1,]    3    4

# Error
cbind(a, b)
#> Error in cbind(a, b): number of rows of matrices must match (see arg 2)

# `a` is first broadcast to have dimensions: (2, 2)
rray_bind(a, b, .axis = 1)
#>      [,1] [,2]
#> [1,]    1    1
#> [2,]    2    2
#> [3,]    3    4

# Error
rbind(a, b)
#> Error in rbind(a, b): number of columns of matrices must match (see arg 2)

# `b` is first broadcast to have dimensions: (2, 2)
rray_bind(a, b, .axis = 2)
#>      [,1] [,2] [,3]
#> [1,]    1    3    4
#> [2,]    2    3    4

Installation

You can install from Github with:

devtools::install_github("r-lib/rray")

Acknowledgements

rray would not be possible without the underlying C++ library, xtensor. Additionally, rray uses a large amount of the infrastructure in vctrs to be as consistent and type stable as possible.

Alternatives

The Matrix package implements a small subset of column-wise broadcasting operations. rray fully supports broadcasting in all operations.

The original motivation for this package, and even for xtensor, is the excellent Python library, NumPy. As far as I know, it has the original implementation of broadcasting, and is a core library that a huge number of others are built on top of.

In the past, the workhorse for flexibly binding arrays together has been the abind package. This package has been a great source of inspiration and has served as a nice benchmark for rray.

Notes

Currently, rray does not handle missing values in arithmetic operations and the reducing functions. This is coming, as the underlying library xtensor natively supports missing values, however a few upstream bugs are currently preventing rray from using those features.

rray will perform best on R 3.6.0 and above. It is able to take advantage of a few of the ALTREP features there, which result in less copies being made per function call.

Copy Link

Version

Monthly Downloads

3

Version

0.0.0.9000

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Davis Vaughan

Last Published

June 29th, 2021

Functions in rray (0.0.0.9000)

is_reshapeable

Check if x can be reshaped
rray

Build a rray object
rray_abs

Absolute value
rray_diag

Create a matrix with x on the diagonal
rray_full_like

Create an array like x
as_matrix

Coerce to a matrix
rray_det

Compute the determinant
rray_sort

Sort an array
rray_split

Split an array along axes
rray_if_else

Conditional selection
rray_extract

Get or set elements of an array by index
rray_mean

Calculate the mean along an axis
rray_dim

Find common dimensions
is_rray

Is x an rray?
rray_ceiling

Rounding functions
rray_dims

Compute the number of dimensions of an object
rray_clip

Bound the values of an array
rray_unique

Find and count unique values in an array
rray_yank

Get or set elements of an array by position
rray_maximum

Maximum and minimum values
rray_flatten

Flatten an array
rray_flip

Flip an rray along a dimension
rray_erf

Error and gamma functions
rray_bind

Combine many arrays together into one array
rray_broadcast

Broadcast to a new dimension
rray_min

Calculate the minimum along an axis
rray_exp

Logarithms and exponentials
rray-compare

Compare arrays
new_rray

Create a new rray
rray_reshape

Reshape an array
rray_rotate

Rotate an array
vec_arith.vctrs_rray

vctrs compatibility functions
rray_square

Power functions
rray_sin

Trigonometric functions
rray_squeeze

Squeeze an rray
rray_min_pos

Locate the position of the minimum value
rray_sign

Sign function
vec_cast_container

Cast to a container type
rray-logical

Logical operators
rray_max

Calculate the maximum along an axis
rray_elems

Compute the number of elements in an array
rray_duplicate

Find duplicated values in an array
rray_max_pos

Locate the position of the maximum value
rray_subset<-

Get or set dimensions of an array
vec_cast_inner

Cast to an inner type
rray_prod

Calculate the product along an axis
rray_multiply_add

Fused multiply-add
rray_sum

Calculate the sum along an axis
vec_type_container

Find the container type of a set of vectors
rray_tile

Tile an array
rray_slice

Get or set a slice of an array
rray_sinh

Hyperbolic functions
rray_transpose

Transpose an array
vec_type_container2

Find the container type of a pair of vectors
vec_type_inner

Find the inner type of a set of vectors
remainder

Remainders
as_rray

Coerce to an rray
common-dim-names

Find common dimension names
pad

Pad missing dimensions when subsetting
as_array

Coerce to an array
dim-names

Dimension names
rray_expand_dims

Insert a dimension into an rray
rray_arith

Arithmetic operations
rray_all_equal

Strictly compare arrays
rray_yank<-

Get or set elements of an array by position
rray_extract<-

Get or set elements of an array by index
rray_slice<-

Get or set a slice of an array
rray_dot

Matrix multiplication
rray_expand

Insert a dimension into an rray
rray_dim_n

Compute the number of dimensions of an object
extremum

Maximum and minimum values
rray_hypot

Compute the square root of the sum of squares
vec_ptype_inner

Find the inner type of a set of vectors
vec_ptype_container2

Find the container type of a pair of vectors
vec_ptype_container

Find the container type of a set of vectors