SparseArray-dim-tuning: Add/drop ineffective dims to/from a SparseArray object

SparseArray-dim-tuningR Documentation

Add/drop ineffective dims to/from a SparseArray object

Description

The ineffective dimensions of an array-like object are its dimensions that have an extent of 1.

Drop all ineffective dimensions from SparseArray object x with drop(x).

Add and/or drop arbitrary ineffective dimensions to/from SparseArray object x with the dim() setter.

Details

The ineffective dimensions of an array-like object are its dimensions that have an extent of 1. For example, for a 1 x 1 x 15 x 1 x 6 array, the ineffective dimensions are its 1st, 2nd, and 4th dimensions.

Note that ineffective dimensions can be dropped or added from/to an array-like object x without changing its length (prod(dim(x))) or altering its content.

drop(x):

Drop all ineffective dimensions from SparseArray derivative x. If x has at most one effective dimension, then the result is returned as an ordinary vector. Otherwise it's returned as a SparseArray derivative.

dim(x) <- value:

Add and/or drop arbitrary ineffective dimensions to/from SparseArray derivative x. value must be a vector of dimensions compatible with dim(x), that is, it must preserve all the effective dimenions in their original order.

See Also

  • drop() in base R.

  • The dim() getter and setter in base R.

  • SparseArray objects.

  • Ordinary array objects in base R.

Examples

## An array with ineffective 1st and 4th dimensions:
a <- array(0L, dim=c(1, 1, 5, 4, 1, 3))
dimnames(a) <- list(NULL, NULL, letters[1:5], NULL, NULL, LETTERS[1:3])
a[c(1:2, 8, 10, 15:17, 20, 24, 40, 56:60)] <- (1:15)*10L
svt <- SparseArray(a)
dim(svt)

## Drop ineffective dims:
dim(drop(svt))  # the 1st, 2nd, and 5th dimensions were dropped

## Drop some ineffective dims and adds new ones:
svt2 <- svt
dim(svt2) <- c(1, 5, 4, 1, 1, 3, 1)
dim(svt2)

## Sanity check:
stopifnot(identical(as.array(drop(svt)), drop(a)))
a2 <- `dim<-`(a, c(1, 5, 4, 1, 1, 3, 1))
dimnames(a2)[c(2, 6)] <- dimnames(a)[c(3, 6)]
stopifnot(identical(as.array(svt2), a2))

Bioconductor/SparseArray documentation built on June 22, 2024, 8:56 p.m.