reduce: Reduce a vector to a single value by iteratively applying a...

View source: R/reduce.R

reduceR Documentation

Reduce a vector to a single value by iteratively applying a binary function

Description

Reduce a vector, .x, to a single value by calling a binary function (a function that takes two values), .f. reduce works by first applying the function to the first two elements of .x, then calling the function on the result of the first computation and the third element of .x, then on the result of that and the fourth element, and so on. Calling reduce on a vector with three elements using the function f is equivalent to calling f(f(x1, x2), x3).

Usage

reduce(.x, .f)

Arguments

.x

A list or vector

.f

A binary (2 argument function), which will be iteratively applied to the vector .x

Examples

# `sum` is just reduce with .f = `+`:

reduce(c(1, 2, 3), `+`)

#> 6

-----------------------------------

# .f = intersect

reduce(
  list(c(2, 6, 3, 4), c(4, 3, 2), c(1, 2, 3)),
  intersect
  )

#> 2 3

-----------------------------------

# .f = `*`

1:10 %>% reduce(`*`)

#> 3628800

cobriant/qelp documentation built on July 1, 2022, 7:24 a.m.