Summarize Subsets of an Object by Splitting by Row and Applying a Summary Function
Source:R/all_generic.R
, R/common.R
split_reduce-methods.Rd
This function summarizes subsets of a numeric matrix or matrix-like object by first splitting the object by row and then applying a summary function.
Implementation of split_reduce for matrix with integer factors.
See split_reduce
for the generic definition and complete documentation.
Implementation of split_reduce for matrix with factor grouping.
See split_reduce
for the generic definition and complete documentation.
Implementation of split_reduce for matrix with factor grouping.
See split_reduce
for the generic definition and complete documentation.
Implementation of split_reduce for NeuroVec objects.
See split_reduce
for the generic definition and complete documentation.
Implementation of split_reduce for NeuroVec objects.
See split_reduce
for the generic definition and complete documentation.
Usage
split_reduce(x, fac, FUN)
# S4 method for class 'matrix,integer,function'
split_reduce(x, fac, FUN)
# S4 method for class 'matrix,factor,missing'
split_reduce(x, fac)
# S4 method for class 'matrix,factor,function'
split_reduce(x, fac, FUN)
# S4 method for class 'NeuroVec,factor,function'
split_reduce(x, fac, FUN)
# S4 method for class 'NeuroVec,factor,missing'
split_reduce(x, fac, FUN)
Arguments
- x
A NeuroVec object
- fac
Factor for grouping
- FUN
Function to apply to each group
Value
A new matrix where the original values have been "reduced" by the supplied function.
Matrix of reduced values
Matrix of reduced values
Matrix of reduced values
Matrix of reduced values
Matrix of reduced values
Details
If 'FUN' is supplied, it must take a vector and return a single scalar value. If it returns more than one value, an error will occur.
If 'x' is a NeuroVec instance, voxels (dimensions 1:3) are treated as columns and time-series (dimension 4) as rows. The summary function is then applied to groups of voxels. However, if the goal is to apply a function to groups of time-points.
This method supports two types of splitting:
By voxel: when length(fac) equals number of voxels
By volume: when length(fac) equals number of volumes
This method supports two types of splitting:
By voxel: when length(fac) equals number of voxels
By volume: when length(fac) equals number of volumes
Examples
mat = matrix(rnorm(100*100), 100, 100)
fac = factor(sample(1:3, nrow(mat), replace=TRUE))
## Compute column means of each sub-matrix
ms <- split_reduce(mat, fac)
all.equal(row.names(ms), levels(fac))
#> [1] TRUE
## Compute column medians of each sub-matrix
ms <- split_reduce(mat, fac, median)
## Compute time-series means grouped over voxels.
## Here, 'length(fac)' must equal the number of voxels: 'prod(dim(bvec)[1:3])'
bvec <- NeuroVec(array(rnorm(24*24*24*24), c(24,24,24,24)), NeuroSpace(c(24,24,24,24), c(1,1,1)))
fac <- factor(sample(1:3, prod(dim(bvec)[1:3]), replace=TRUE))
ms <- split_reduce(bvec, fac)
ms2 <- split_reduce(bvec, fac, mean)
all.equal(row.names(ms), levels(fac))
#> [1] TRUE
all.equal(ms, ms2)
#> [1] TRUE