Iterators — object-oriented programming constructs that act as a pointer in an ordered sequence — are familiar to programmers of languages like Python, but are not a standard part of the R language. Nonetheless, by installing the iterators package (an open-source contribution by Revolution Analytics) you can create and manipulate iterator objects in R.
If you're new to iterators, Exegetic Analytics provides an in-depth tutorial on iterators in R, with worked examples of using iterators to:
- loop over rows/columns of matrices and data frames
- iterate over lines in a file
- filter a sequence (say, to select only primes)
- create custom iterators to generate sequences (e.g. the Fibonacci sequence)
In addition to providing a more elegant was to program with sequences, iterators in R also make it easy to parallelize R code using the foreach package.
Exegetic Analytics: Iterators in R