The whole point of ifelse
, in contrast to if
, is that it’s vectorised: its result will be of the same length as the test. Since your test expression is of length 1 (= a single value), so will the result be a singe value. In addition, ifelse
strips attributes (including the class)1. According to the documentation:
The mode of the result may depend on the value of ‘test’ (see the
examples), and the class attribute (see ‘oldClass’) of the result
is taken from ‘test’ and may be inappropriate for the values
selected from ‘yes’ and ‘no’.
See also the examples there.
The only reason to use ifelse
is when you specifically want vectorisation. For a single test value, use if
. The idiomatic way of writing this in R would be as follows:
if (inherits(im, 'cimg')) im = boats
Or maybe:
im = if (inherits(im, 'cimg') im else boats
There are two additional things of note in this code:
x %in% 'foo'
is the same as x == 'foo'
. Using %in%
here is unnecessary and confusing (because %in%
implies that we’re matching x
against a vector of more than one values).
- It’s virtually always better to use
inherits
for the test you’re trying to perform, rather than manually checking the value of class
.
1 In my opinion that’s a defect (even though it’s documented). And others clearly agree, which is why if_else
in ‘dplyr’ behaves sensibly.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…