14  Answering one way or another

when asks a question and gives the answer beside it, then asks the next one.

pupils <- data.frame(
  name = c("ann", "bob", "cat"),
  score = c(95, 75, 50),
  stringsAsFactors = FALSE
)

pupils |> add(band = when(score >= 90, "A", score >= 70, "B", otherwise = "C"))
name score band
ann 95 A
bob 75 B
cat 50 C
pupils = pd.DataFrame({
    "name": ["ann", "bob", "cat"],
    "score": [95, 75, 50],
})

pupils >> add(band = when(col.score >= 90, "A", col.score >= 70, "B", otherwise = "C"))
name score band
ann 95 A
bob 75 B
cat 50 C

The arguments come in pairs: a question, then what it gives. The first question that is true wins, so the order is part of what the sentence means. Ask the same two the other way round and the answers change, which is not a trap but the thing you are choosing when you write them in an order:

pupils |> add(band = when(score >= 70, "B", score >= 90, "A", otherwise = "C"))
name score band
ann 95 B
bob 75 B
cat 50 C
pupils >> add(band = when(col.score >= 70, "B", col.score >= 90, "A", otherwise = "C"))
name score band
ann 95 B
bob 75 B
cat 50 C

ann scored 95, met the first question that was asked, and got a B.

Leave out otherwise and a row that matched nothing is missing:

pupils |> add(top = when(score >= 90, "yes"))
name score top
ann 95 yes
bob 75 NA
cat 50 NA
pupils >> add(top = when(col.score >= 90, "yes"))
name score top
ann 95 yes
bob 75 NaN
cat 50 NaN

Every answer has to be the same kind of thing, because they all end up in one column:

collect(pupils |> add(band = when(score >= 90, "A", otherwise = 0)))
Error:
! 
illegal: `when` gives one column, so all of its answers have to be the same kind of thing. One of them is text and this is a number
  |
2 |   then add [band] as when(([score] >= 90), "A", otherwise 0)
  |                                                           ^
try:
    collect(pupils >> add(band = when(col.score >= 90, "A", otherwise = 0)))
except GodError as refusal:
    print(refusal)

illegal: `when` gives one column, so all of its answers have to be the same kind of thing. One of them is text and this is a number
  |
2 |   then add [band] as when(([score] >= 90), "A", otherwise 0)
  |                                                           ^

Python has a conditional of its own, and it is worth saying why god does not use it. Writing "A" if col.score >= 90 else "B" would decide the answer once, while the pipeline was being built, and throw the question away. Nothing would report a problem. R has no such expression at all. So the word is when in both, and it is the same word in the text form.