Does R have something equivalent to reduce() in Python?

Derrick Zhang picture Derrick Zhang · Sep 14, 2011 · Viewed 8.3k times · Source

That is : "Apply function of two arguments cumulatively to the items of sequence, from left to right, so as to reduce the sequence to a single value. "

Answer

Andrie picture Andrie · Sep 14, 2011

Yes, it's called Reduce.

An example:

Reduce(paste, LETTERS[1:5])
[1] "A B C D E"

Reduce(sum, 1:5)
[1] 15

#List arguments work the same
Reduce(sum, list(1, 2, 3, 4, 5))
[1] 15

For more information about functional programming in R see the help file for ?funprog, an alias for ?Reduce