Learn R Programming

lambda.tools (version 1.0.9)

map: Apply a function over each element of a vector

Description

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.

References

Rowe, Brian Lee Yung. Modeling Data With Functional Programming In R. Chapman & Hall/CRC Press. Forthcoming.

See Also

fold maprange mapblock

Examples

Run this code
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)

Run the code above in your browser using DataLab