Iterations in R

In this third lesson of our interactive control flow, iteration, and functions course, you'll learn about iterating through elements of your dataset using R code.

This lesson will teach you how to write for loops in R, while loops in R, and even give you a second approach to writing for loops. But perhaps more importantly, it will also cover why we might want to use iterators in the context of data analysis, and when iterators are the wrong choice.

Over the course of the lesson, you'll write for loops and while loops to iterate through a real dataset using real R code, but you'll also learn about the differences between iteration and vectorization, and when you might choose to use one over the other.

After you finish this lesson, you will be comfortable with iterations in R, including for loops and while loops, and you'll have a solid grasp of when and why they should be used (and just as important, when they shouldn't).


  • Learn how to use for loops in R.
  • Learn how to use while loops in R.
  • Understand the differences between iteration and vectorization.

Mission Outline

  1. Introduction
  2. Motivation for Iteration
  3. The for Loop
  4. Another Way to Write for Loops
  5. Intuition Behind the while Loop
  6. A while Loop Example
  7. Your Own while Loop
  8. Iteration vs. Vectorization
  9. Next Steps

Course Info:


This course requires a Basic subscription and includes three missions and one guided project.  It is the third course in the Data Analyst in R learning path.


Take a Look Inside