Tsallaka zuwa abun ciki
Justice Tech School

if and else

if takes a single TRUE or FALSE. ifelse is the vectorised version.

Example
x <- 7
if (x > 5) {
  print("big")
} else if (x > 2) {
  print("medium")
} else {
  print("small")
}
print(ifelse(c(1, 6, 3) > 2, "hi", "lo"))

Exercise

Vectorised if.

(cond, "a", "b")

Quiz Test

Where must else sit?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.