Learn R Programming

slam (version 0.1-3)

rollup: Rollup Sparse Arrays

Description

Rollup (aggregate) sparse arrays along arbitrary dimensions.

Usage

rollup(x, MARGIN, INDEX, FUN, ...)

## S3 method for class 'simple_triplet_matrix': rollup(x, MARGIN, INDEX = NULL, FUN = sum, \dots) ## S3 method for class 'simple_sparse_array': rollup(x, MARGIN, INDEX = NULL, FUN = sum, \dots, DROP = FALSE, EXPAND = c("none", "sparse", "dense", "all")) ## S3 method for class 'matrix': rollup(x, MARGIN, INDEX = NULL, FUN = sum, \dots, DROP = FALSE) ## S3 method for class 'array': rollup(x, MARGIN, INDEX = NULL, FUN = sum, \dots, DROP = FALSE)

Arguments

x
a sparse (or dense) array, typically of numeric or logical values.
MARGIN
a vector giving the subscripts (names) of the dimensions to be rolled up.
INDEX
a corresponding (list of) factor (components) in the sense that as.factor defines the grouping(s). If NULL collapses the corresponding dimension(s) (default).
FUN
the name of the function to be applied.
DROP
option to delete the dimensions of the result which have only one level.
EXPAND
the cell expansion method to use (see Details).
...
optional arguments to FUN.

Value

  • An object of the same class as x where for class simple_triplet_matrix the values are always of type double if FUN = sum (default).

    The dimnames corresponding to MARGIN are based on (the components of) INDEX.

Details

Provides fast summation over the rows or columns of sparse matrices in simple_triplet-form.

If (a component of) INDEX contains NA values the corresponding parts of x are omitted.

For simple_sparse_array the following cell expansion methods are provided: [object Object],[object Object],[object Object],[object Object] Note that the memory and time consumption increases with the level of expansion.

Note that the default method tries to coerce x to array.

See Also

simple_triplet_matrix and simple_sparse_array for sparse arrays.

apply for dense arrays.

Examples

Run this code
##
x <- matrix(c(1, 0, 0, 2, 1, NA), nrow = 2, 
	    dimnames = list(A = 1:2, B = 1:3))
x
apply(x, 1L, sum, na.rm = TRUE)
##
rollup(x, 2L, na.rm = TRUE)
##
rollup(x, 2L, c(1,2,1), na.rm = TRUE)
## omit
rollup(x, 2L, c(1,NA,1), na.rm = TRUE)
## expand
a <- as.simple_sparse_array(x)
a
r <- rollup(a, 1L, FUN = mean, na.rm = TRUE, EXPAND = "dense")
as.array(r)
## 
r <- rollup(a, 1L, FUN = function(x, nz) 
	length(x) / (length(x) + nz), 
    EXPAND = "sparse"
)
as.array(r)

Run the code above in your browser using DataLab