Iterators in R: a tutorial

[This article was first published on Revolutions, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)