collapse: Collapse tagged arrays

View source: R/tagged.array.R

collapseR Documentation

Collapse tagged arrays

Description

Interface to the apply function for tagged arrays

Usage

  collapse(object, drop.tags, keep.tags, FUN, tag.result=FALSE, ...)

Arguments

object

An array with a dimtags attribute

drop.tags

tags of dimensions to be dropped

keep.tags

tags of dimensions to be preserved

FUN

the function to be applied

tag.result

binary indicator. If TRUE then the result will also be a tagged array if possible

...

additional arguments passed to FUN

Details

Using the apply function to simplify high-dimensional arrays can be complicated. This function provides a simplified interface for tagged arrays with two advantages over the basic apply function:

  1. Dimensions are referred to by name (dimtags), not by number.

  2. The user can define ether the dimensions to be kept or the dimensions to be dropped (but not both).

Value

Usually the output is a lower-dimensional array (vector, matrix, or array). If the output cannot be simplified because FUN returns values of varying length, the output will be a list.

If tag.result=TRUE, then the collapse function will add dimtags to the result if it can. This is generally only possible if FUN returns a scalar and the output is not a vector (which has no dim attribute).

Tagging the result allows a further call to the collapse function. For example, if MCMC output is represented as a tagged array, we may wish to calculate the posterior mean within each chain, and then the variance of the means between chains.

Author(s)

Martyn Plummer

See Also

dimtags, apply


rjags documentation built on Nov. 21, 2026, 9:10 a.m.