freduce | R Documentation |
Apply a binary function iteratively over a list or vector, reducing it to a single value or a sequence of intermediate results. This is a wrapper around [Reduce()] that supports optional initial values, right-to-left evaluation, accumulation of intermediate steps, and output simplification.
freduce(
.x,
.f,
.init = NULL,
.right = FALSE,
.accumulate = FALSE,
.simplify = TRUE
)
.x |
A vector or list to reduce. |
.f |
A binary function to apply. Can be given as a function or quoted (e.g., '\'+\“). |
.init |
Optional initial value passed to [Reduce()]. If 'NULL', reduction starts from the first two elements. |
.right |
Logical. If 'TRUE', reduction is performed from right to left. |
.accumulate |
Logical. If 'TRUE', returns a list of intermediate results (like a scan). |
.simplify |
Logical. If 'TRUE' and all intermediate results are length 1, the output is simplified to a vector. |
A single value (default) or a list/vector of intermediate results if '.accumulate = TRUE'.
freduce(1:5, `+`) # => 15
freduce(letters[1:4], paste0) # => "abcd"
freduce(list(1, 2, 3), `*`) # => 6
freduce(1:3, `+`, .init = 10) # => 16
freduce(1:3, paste0, .right = TRUE) # => "321"
freduce(1:4, `+`, .accumulate = TRUE) # => c(1, 3, 6, 10)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.