In this first lesson of our interactive control flow, iteration, and functions course, we'll dive into how you can control what happens with your code by using conditionals.

Specifically, you'll learn about the if statement, the if_else() function, and the case_when() function in R. You'll write functions that control data processing based on conditions, and build complexity by using multiple conditions.

The ability to create these kinds of flows is critical in R programming. Mastering control flow gives you powerful tools for sorting and processing your data based on any condition or conditions you set.

After you finish this lesson, you will be comfortable with if statements, the if_else() and case_when() functions in R, and with building complex conditionals using more than one condition at a time.

Objectives

  • Learn about if statements.
  • Learn to use the if_else() function and the case_when() functions.
  • Combine conditions for more complex flows.

Lesson Outline

  1. Introduction
  2. Review of Comparison Operators
  3. The if Statement
  4. The if_else() Function
  5. Moving Beyond Two Branches
  6. Using Multiple Conditions
  7. A Technical Note on Multiple Conditions
  8. The case_when() Function
  9. Other Useful Comparison Operators
  10. Next Steps