Learn R Programming

slam (version 0.1-21)

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, FUN = sum, \dots) ## S3 method for class 'simple_sparse_array': rollup(x, MARGIN, INDEX, FUN = sum, \dots) ## S3 method for class 'matrix': rollup(x, MARGIN, INDEX, FUN = sum, \dots) ## S3 method for class 'array': rollup(x, MARGIN, INDEX, FUN = sum, \dots)

Arguments

x
a sparse array containing numeric, integer, 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(f) defines the grouping.
FUN
the name of the function to be applied.
...
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. The dimnames are taken from (the components of) INDEX.

Details

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

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

See Also

simple_triplet_matrix and simple_sparse_array for sparse 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
zz <- rollup(x, 2L, c(1,2,1), na.rm = TRUE)
zz
##
s <- as.simple_triplet_matrix(x)
z <- rollup(s, 2L, c(1,2,1), na.rm = TRUE)
identical(zz, as.matrix(z))
##
a <- as.simple_sparse_array(s)
z <- rollup(a, 2L, c(1,2,1), na.rm = TRUE)
identical(as.array(zz), as.array(z))
##
zz <- rollup(x, 2L, c(1,NA,1), na.rm = TRUE)
zz
##
z  <- rollup(s, 2L, c(1,NA,1), na.rm = TRUE)
identical(zz, as.matrix(z))
##
z  <- rollup(a, 2L, c(1,NA,1), na.rm = TRUE)
identical(as.array(zz), as.array(z))

Run the code above in your browser using DataLab