mapreduce: Apply function over collection and then iteratively reduce it...

Description Usage Arguments Details See Also Examples

View source: R/mapreduce.R

Description

Apply function over collection and then iteratively reduce it a la' mapreduce() from Julia.

Usage

1
2
3
mapreduce(f, o, x, y = NULL, ...)
mrchop(f, o, x, m, ...)
reducechop(o, x, m, ...)

Arguments

f

A function to apply to the collection.

o

A binary operator. Typically arithmetic operators, but can be another (binary) function.

x

A collection, such as a a list, matrix, or data frame.

y

A list of additional arguments from f. Passed to MoreArgs from Map.

...

Arguments passed to Reduce.

m

Margin. 1 for rows, 2 for columns.

Details

A simplified mapreduce from Julia with a multivariate option.

Base R has functionals that output a list by way of lapply() and Map (among others), while Reduce diminishes given elements in a consecutive manner until a single result remains. To reduce the output of a mapping, the mentioned functions are required. In turn, mapreduce simplifies this process.

The functions mrchop and reducechop have similar properties. The former applies mapreduce row-wise or column-wise, which can be specified; the latter applies Reduce in the same manner.

See Also

https://github.com/robertschnitman/afp, Map, Reduce, mapreduce from Julia:https://docs.julialang.org/en/v0.6.1/stdlib/collections/#Base.mapreduce-NTuple{4,Any}

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 1. mapreduce: apply a function to 3 matrices and consecutively divide them.
matrixl <- list(A = matrix(c(1:9), 3, 3), B = matrix(10:18, 3, 3), C = matrix(19:27, 3, 3))
output1 <- mapreduce(function(x) x^2 + 1, `/`, matrixl) 
output1 # matrix 3x3

# 2. mapreduce: use multiple arguments.
matrixl <- list(A = matrix(c(1:9), 3, 3), B = matrix(10:18, 3, 3), C = matrix(19:27, 3, 3))
output2 <- with(matrixl, mapreduce(function(i, j, k) i*j - k, `/`, A, list(B, C)))
output2

# 3. mrchop.
mrchop(function(x) x/2, `+`, mtcars, 1)

# 4. reducechop.
reducechop(`+`, mtcars, 2) # Column-wise (default).
reducechop(`/`, mtcars, 1) # Row-wise. Equivalent to Reduce(`/`, mtcars).  

robertschnitman/afp documentation built on Aug. 9, 2020, 9:46 a.m.