funprog
Common Higher-Order Functions

Description

Performs high-order operations for filtering, finding, mapping, negating, or reducing.

Usage

Filter(f, x)
Find(f, x, right = FALSE, nomatch = NULL)
Map(f, ...)
Negate(f)
Reduce(f, x, init, right = FALSE, accumulate = FALSE)
Position(f, x, right = FALSE, nomatch = NA_integer_)

Arguments

f a function to be applied to x. f contains only one argument for Filter, Find and Position. It contains two arguments for Reduce. It contains more arguments for Map (depending on the ... argument). For Negate, f must return a logical value.
x any vector, matrix, or list object to which to apply f.
right a logical value. If TRUE, the operation is performed from right to left. If FALSE (the default), the operation is performed from left to right.
nomatch the value to return when there is no match for Find and Position.
init the initial value for which to apply Reduce. If init is missing, the first element or the last element (if right is TRUE) of x is used as the initial value.
accumulate a logical value. If TRUE, the accumulated results of each Reduce step are returned. If FALSE (the default), only the result of the last Reduce step is returned.
... More arguments like x.

Details

Filter extracts only the elements of x on which f is applied and returns TRUE.
Find and Position return the first matched element and its position, respectively.
Map applies the function f to multiple list or vector arguments.
Negate returns the negation of the function f.
Reduce use the function f to successively combine the element of x or init from left to right, or from right to left(if right is TRUE).
Value
Filterreturns a subset of x.
Findreturns the first matched element of x, or the specified value for nomatch.
Positionreturns an integer of the position of the matched element of x, or the specified value for nomatch.
Mapreturns a list, vector, or matrix, which is the return value of mapply(f, ..., SIMPLIFY = FALSE).
Negatereturns a logical value.
Reducereturns the combination of last reduce step, or the accumulated combinations of each reduce step, in a vector.
Note
Many of these operations are done more efficiently by using the vectorized functions and subscripting operators in Spotfire Enterprise Runtime for R.
See Also
sapply, mapply, rev
Examples

# Filter out non-positive values in sin(1:10) Filter(function(x) x > 0, sin(1:10))

# Find the first non-zero result of "sin(x)" from left to right Find(sin, c(0, -1, 0, -2, 1, 4))

# Find the first non-zero result of "sin(x)" from right to left Find(sin, c(0, -1, 0, -2, 1, 4), right = TRUE)

# Find the position of first non-zero result of "sin(x)" from left # to right Position(sin, c(0, -1, 0, -2, 1, 4))

# Find the position of first non-zero result of "sin(x)" from right to left Position(sin, c(0, -1, 0, -2, 1, 4), right = TRUE)

unlist(Map(function(str)max(strsplit(str,split="")[[1]]), month.abb))

Negate(is.na)(c(1, 2, 3, NA, 4, 5, NA_integer_, 7))

Reduce("+", c(1, 4, -1, 3, 2, 0, 5)) Reduce("+", c(1, 4, -1, 3, 2, 0, 5), accumulate = TRUE)

Package base version 6.1.1-7
Package Index