Learn R Programming

eList (version 0.0.1.0)

helpersFun: Higher Order Helpers for Vector Comprehension

Description

These functions use a vector and a function to create an iterable object that can be used for vector comprehension.

Usage

starmap(x, f, axis = 0, ..., longest = TRUE, fill = NULL)

starred(x, f, axis = 0, ..., longest = TRUE, fill = NULL)

starfilter(x, f, axis = 0, ..., longest = TRUE, fill = NULL)

partition(x, f, ...)

dropwhile(x, f, ...)

takewhile(x, f, ...)

Arguments

x

vector

f

function to be applied to x

axis

which axis to perform different operations? axis=0, the default, performs operations on each element in the list (columns), while axis=1 performs operations on each object within the elements of a list (rows).

...

additional arguments passed to lower functions. See funprog

longest

logical; should the longest item be used to determine the new length or shortest? Defaults to TRUE.

fill

object with which to fill the vector when operating on elements with varying lengths or shifts.

Value

list or other vector

Functions

  • starmap: Use map f on each element of x.

  • starred: Use reduce f on each element of x.

  • starfilter: Use filter f on each element of x.

  • partition: Map predicate function f to each object in x and split based on which items evaluate to TRUE (index 1) vs. FALSE (index 2).

  • dropwhile: Drop objects from x until predicate function f evaluates to FALSE.

  • takewhile: Keep objects from x until predicate function f evaluates to FALSE.

Details

The star functions are similar to their funprog counterparts, except that they are applied one level deeper in the list.

Examples

Run this code
# NOT RUN {
x <- list(1:3, 4:6, 7:9)

## filter away values less than 6
starfilter(x, ~.i > 5)
starfilter(x, ~.i > 5, axis=1) # Transposed

starred(x, `/`, init=1) # sequentially divide each item, starting at 1

partition(x, ~.i > 5)
# }

Run the code above in your browser using DataLab