By Thoralf Mildenberger (ZHAW)
Everybody who knows a bit about
R knows that in general loops are said to be evil and should be avoided, both for efficiency reasons and code readability, although one could argue about both.
The usual advice is to use vector operations and
apply() and its relatives.
lapply() work by applying a function on each element of a vector or list and return a vector, matrix, array or list of the results.
apply() applies a function on one of the dimensions of a matrix or array and returns a vector, matrix or array. These are very useful, but they only work if the function to be applied to the data can be applied to each element independently of each other.
There are cases, however, where we would still use a
for loop because the result of applying our operation to an element of the list depends on the results for the previous elements. The
R base package provides a function
Reduce(), which can come in handy here. Of course it is inspired by functional programming, and actually does something similar to the Reduce step in
MapReduce, although it is not inteded for big data applications. Since it seems to be little known even to long-time
R users, we will look at a few examples in this post. Continue reading