Iterators in R: a tutorial

November 18, 2013

(This article was first published on Revolutions, and kindly contributed to R-bloggers)

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

To leave a comment for the author, please follow the link and comment on their blog: Revolutions. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)