typestar

reduce and accumulate in R

Folding a list into one value, or keeping every step.

library(purrr)
library(dplyr)

print(reduce(1:5, `+`))
print(reduce(1:5, `+`, .init = 100))
print(accumulate(1:5, `+`))

tables <- list(
  tibble(lang = c("r", "python"), tpm = c(96, 104)),
  tibble(lang = c("r", "python"), tours = c(4L, 13L)),
  tibble(lang = c("r", "python"), stars = c(3L, 3L))
)
print(reduce(tables, left_join, by = "lang"))
print(reduce(list(c(1, 2), c(2, 3), c(2, 4)), intersect))

How it works

  1. reduce takes an optional starting value with .init.
  2. accumulate returns the running results.
  3. Reducing with a join is how you combine many tables.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
397
Tokens
163
Three-star pace
105 tpm

At the three-star pace of 105 tokens a minute, this run takes about 93 seconds.

Type this snippet

Step 4 of 4 in purrr, step 4 of 10 in Functional & text tools.

← Previous Next →