3.5 Selecting elements based on a condition: ifelse

We will see if ... else ... later

ifelse follow this syntax:

ifelse(l, t, f): ie if l is TRUE do t / if l is FALSE do f

z <- rnorm(6)
ifelse(z >= 0, z, - z ) # yup abs(z)
## [1] 0.3911103 2.0682086 1.1948591 0.3259063 0.2230448 0.8427713

it applies the recycle rules:

x <- rnorm(6)
ifelse(x > 0, x^2, 0)
## [1] 3.32601519 0.00000000 0.02115266 0.26537366 0.13450839 0.00000000

Important: all elements are evaluated before knowing which are selected! (that explain we have a warning sometimes instead of an error)