← Back to R Course | Chapter 4: Control Flow | Lesson 2 of 6

ifelse()

In this page:

  1. ifelse()

ifelse()

ifelse(condition, yes, no) is a vectorized version of if/else, evaluating a condition element-wise across a whole vector and returning a vector of results. This avoids writing a loop to apply conditional logic to every element. It's one of R's most common idioms for transforming data based on a condition.

Note: Use ifelse() when you need to apply a condition across an entire vector at once, not just a single value.

Example: ifelse()

markup
scores <- c(95, 60, 78, 40, 88)

results <- ifelse(scores >= 70, "Pass", "Fail")
print(results)
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.