apply_3d: apply function for 3d array

View source: R/arr_3d.R

apply_3dR Documentation

apply function for 3d array

Description

NA values will be removed automatically

Usage

apply_3d(
  array,
  dim = 3,
  FUN = rowMeans2,
  by = NULL,
  scale = 1,
  na.rm = TRUE,
  ...
)

Arguments

array

A 3d array

dim

giving the subscripts to split up data by.

FUN

function, should only be row applied function, e.g. matrixStats::rowMeans2, matrixStats::rowMins, matrixStats::rowRanges. Because 3d array will be convert to matrix first, with the aggregated dim in the last dimension.

by
  • If not provided (NULL), the aggregated dim will be disappear. For example, daily precipitation ⁠[nrow, ncol, 31-days]⁠ aggregate into monthly ⁠[nrow, ncol]⁠.

  • If provided, by should be equal to the aggregated dim. For example, daily precipitation ⁠[nrow, ncol, 365-days]⁠ aggregate into monthly ⁠[nrow, ncol, 12-months]⁠. In that situation, by should be equal to 365, and be format(date, '%Y%m').

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.

See Also

apply_row matrixStats::rowRanges

Examples

set.seed(1)
size <- c(10, 8, 31)
arr <- array(rnorm(10 * 8 * 31), dim = size)

by <- c(rep(1, 10), rep(2, 21))
r2 <- apply_3d(arr, 3, by = by, FUN = rowMeans)

## Not run: 
arr_yearly <- apply_3d(arr, by = year(dates), scale = days_in_month(dates))

## End(Not run)

rtrend documentation built on June 22, 2024, 11:39 a.m.