All it does is take a vector of T/F, then returns values from two other vectors depending on the case. It's also possible to just return one value for either T or F, as you are doing here (the sum or 0).

Example:

# Vector input, one possible true of false answer
ifelse((1:5 %% 2) == 0, "even", "odd")
#> [1] "odd" "even" "odd" "even" "odd"
# Vector input, pick corresponding index value from true or false vectors
ifelse((1:5 %% 2) == 0, letters[1:5], LETTERS[1:5])
#> [1] "A" "b" "C" "d" "E"

^{Created on 2022-04-18 by the reprex package (v2.0.1)}

My answer if just helping you fix your current error, but I'm not 100% sure if your code captures the function one you provided, so please check this.

Thank you sm! The solution you give does fix the error. Unfortunately, like you said, your code does not capture the function I provided. It's ok. I'll figure it out.