Suharto Anggono Suharto Anggono
2016-Nov-27 01:50 UTC
[Rd] ifelse() woes ... can we agree on a ifelse2() ?
For S Ellison, just clarifying, I am Suharto Anggono, not Martin Maechler. "Martin et al.," from my previous E-mail was the beginning of message from Gabriel Becker, that I quoted. The quoted "still a bit disappointed that nobody has taken a look" is from Martin Maechler. In all of the proposed 'ifelse'-like functions so far, including from me (that I labeled as 'ifelse2', following Martin Maechler) and from Martin Maechler, the length of the result equals the length of 'test', like in 'ifelse'. There is no recycling of 'test'. -----------------------------------------> Just stating, in 'ifelse', 'test' is not recycled. As I said in "R-intro: length of 'ifelse' result" > (https://stat.ethz.ch/pipermail/r-devel/2016-September/073136.html), ifelse(condition, a, b) > returns a vector of the length of 'condition', even if 'a' or 'b' is longer.That is indeed (almost) the documented behaviour. The documented behaviour is slightly more complex; '... returns a value _of the same shape_ as 'test''. IN principle, test can be a matrix, for example.> A concrete version of 'ifelse2' that starts the result from 'yes': > .. still a bit disappointed that nobody has taken a look ...I took a look. The idea leaves (at least) me very uneasy. If you are recycling 'test' as well as arbitrary-length yes and no, results will become frighteningly hard to predict except in very simple cases where you have well-defined and consistent regularities in the data. And where you do, surely passing ifelse a vetor of the right length, generated by rep() applied to a short 'test' vector, will do what you want without messing around with new functions that hide what you're doing. Do you really have a case where 'test' is neither a single logical (that could be used with 'if') nor a vector that can be readily replicated to the desired length with 'rep'? If not, I'd drop the attempt to generate new ifelse-like functions. S Ellison
Maybe Matching Threads
- ans[nas] <- NA in 'ifelse' (was: ifelse() woes ... can we agree on a ifelse2() ?)
- ifelse() woes ... can we agree on a ifelse2() ?
- ifelse() woes ... can we agree on a ifelse2() ?
- ifelse() woes ... can we agree on a ifelse2() ?
- ifelse() woes ... can we agree on a ifelse2() ?