• apply_col: aggregate by col, return a [ngrp, ncol] matrix

  • apply_row: aggregate by row, return a [nrow, ngrp] matrix

apply_col(mat, by, FUN = colMeans2, scale = 1, ...)

apply_row(mat, by, FUN = rowMeans2, scale = 1, ...)

Arguments

mat

matrix, [nrow, ncol]

by

integer vector, with the dim of [ntime]

scale

in the same length of by, or a const value, value_returned = FUN(x)*scale. This parameter is designed for converting monthly to yearly, meanwhile multiply days in month. Currently, same group should have the same scale factor. Otherwise, only the first is used.

Details

For example, setting the dimension of mat is [ngrid, ntime], if you want to aggregate by time, apply_row should be used here; if you want to aggregate by region (grids), apply_col should be used.

Note

This function also suits for big.matrix object.

Examples

mat <- matrix(rnorm(4*6), 4, 6)
mat_bycol <- apply_col(mat, c(1, 1, 2, 2), colMeans)
mat_byrow <- apply_row(mat, c(1, 1, 2, 2, 3, 3), rowMeans)