This function implements a map operation over arbitrary indexable
data structures. Both 1D and 2D data structures are supported.
Arguments
x
Any indexable data structure
fn
A function applied to each x_i in x
acc
An initial data structure to accumulate the value of f(x_i)
Value
A sequence representing < f(x_i) > for all x_i in x
Usage
map(x, fn, acc) %::% . : Function : . : .
map(x, fn, acc=c())
Details
While many functions in R are vectorized, some functions only work
for scalar input. The map function transforms any scalar-valued
function into a vectorized function. This is known as the
map-equivalent form of the scalar function.
The map operation is implemented for 2D data structures as a
column-based operation. If a row-based procedure is desired instead,
simply transpose the data structure.
Conceptually, the map operation is equivalent to the apply family
of functions. The reason for this implementation is primarily for
pedagogical purposes.
Note
This function is implemented using recursion and will
throw an error if the length of x approaches
getOption('expressions') / 8.0. This limit
is due to R attempting to protect against infinite recursion.
See options for more details.
Author(s)
Brian Lee Yung Rowe
References
Rowe, Brian Lee Yung.
Modeling Data With Functional Programming In R. Chapman & Hall/CRC Press.
Forthcoming.
See Also
foldmaprangemapblock
Examples
map(-10:10, quantize)
# Output a list instead of a vector
map(-10:10, quantize, acc=list())
# Sum the columns of a matrix
map(matrix(1:24, ncol=4), sum)
# Sum the columns of a data.frame
map(data.frame(a=1:6, b=7:12, c=13:18, d=19:24), sum)