foldblock: Successively apply a function to adjacent blocks of a...

Description Arguments Value Usage Details Author(s) See Also Examples

Description

Apply a function to non-overlapping sub-sequences and the accumulated value of the function application

Arguments

x

Any indexable data structure

window

The number of elements in each sub-sequence

fn

The function applied to the sub-sequence

acc

The intermediate accumulated value

Value

The accumulated value

Usage

foldblock(x, window, fn, acc=0)

Details

This function is the fold counterpart of mapblock. Like mapblock the usefulness of this function is for the 2D case, as it can simplify interacting with matrices. See the example below for using foldblock as a summation operator over matrices

Author(s)

Brian Lee Yung Rowe

See Also

map fold foldrange

Examples

1
2
3
4
5
6
7
8
# Sum 5 2 x 2 matrices
ms <- matrix(sample(40,20, replace=TRUE), nrow=2)
foldblock(ms,2, function(a,b) a + b)

# 1D foldblock is equivalent to 2D fold
x <- 1:12
f <- function(a,b) mean(a) + b
foldblock(x,3,f) == fold(matrix(x, nrow=3),f, 0)

lambda.tools documentation built on May 2, 2019, 4:28 a.m.