ifelse()
In this page:
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()
scores <- c(95, 60, 78, 40, 88)
results <- ifelse(scores >= 70, "Pass", "Fail")
print(results)
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: