Working With Control Structures

In the Introduction to Programming in R course, you wrote code that ran sequentially, one line after the other. This is the "default" mode in which code is executed in R and other programming languages.

As a data scientist, however, you won't always want your code to be run sequentially. You'll want to perform a calculation only if a condition is met, or you'll want to repeatedly apply a function to elements of an object using a for loop in R.

In this lesson, you'll use control structures, methods for directing the order in which operations are performed, to manipulate and analyze FiveThirtyEight's data on the 2014 FIFA World Cup. In addition, you will learn that control structures are powerful tools and can do even more when combined together.

Control structures such as if-else statements and for loops can often be confusing concepts. After you complete this lesson and you are still confused about how they are used, you can always come back to this tutorial to get a firmer grasp on this topic.


  • Learn to write if-else statements.
  • Learn to use for-loops.
  • Learn to make use of nested control structures.

Lesson Outline

1. Introduction to Control Structures
2. Importing the Data
3. Selection: Writing Conditional Statements
4. Repetition: Writing For-Loops
5. Looping Over Rows of a Data Frame
6. Nested Control Structures
7. Storing For-Loop Output in Objects
8. More Than Two Cases
9. More Than Two Cases: Writing a For-Loop
10. Next Steps
11. Takeaways

Take a Look Inside