What is puzzling me is that rnorm(1) is only evaluated *twice*, one time for each branch, with only 2 different random deviates, instead of giving ten different random deviates. y1 has indeed 10 values but with only 2 different ones. I would like to have rnorm be evaluated for each row and collect ten *different* random deviates. y1 [1] 0.4087172 0.7707796 0.4087172 0.4087172 0.7707796 0.4087172 0.4087172 [8] 0.7707796 0.7707796 0.4087172 Thanks. Jacques.
ifelse question
5 messages · Jacques Ropers, Peter Dalgaard, Martin Maechler +1 more
jropers at freesurf.fr wrote:
What is puzzling me is that rnorm(1) is only evaluated *twice*, one time for each branch, with only 2 different random deviates, instead of giving ten different random deviates. y1 has indeed 10 values but with only 2 different ones.
I find it more puzzling why you expect that ifelse, a function of three vector arguments, would cause its input arguments to be reevaluated for every element of the result.
I would like to have rnorm be evaluated for each row and collect ten *different* random deviates. y1 [1] 0.4087172 0.7707796 0.4087172 0.4087172 0.7707796 0.4087172 0.4087172 [8] 0.7707796 0.7707796 0.4087172 Thanks. Jacques.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
...ifelse, a function of three **vector** arguments.... Yes !! I misunderstood the functioning of ifelse. Thanks Jacques.
Peter Dalgaard wrote:
jropers at freesurf.fr wrote:
What is puzzling me is that rnorm(1) is only evaluated *twice*, one time for each branch, with only 2 different random deviates, instead of giving ten different random deviates. y1 has indeed 10 values but with only 2 different ones.
I find it more puzzling why you expect that ifelse, a function of three vector arguments, would cause its input arguments to be reevaluated for every element of the result.
I would like to have rnorm be evaluated for each row and collect ten *different* random deviates. y1 [1] 0.4087172 0.7707796 0.4087172 0.4087172 0.7707796 0.4087172 0.4087172 [8] 0.7707796 0.7707796 0.4087172 Thanks. Jacques.
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
"jropers at freesurf" == jropers at freesurf fr <jropers at freesurf.Fr>
on Tue, 12 Dec 2006 22:24:33 +0100 writes:
jropers at freesurf> ...ifelse, a function of three **vector**
jropers at freesurf> arguments.... Yes !! I misunderstood the
jropers at freesurf> functioning of ifelse.
Seems to happen more an more often.
When I teach "R programming" I nowadays usually emphasize that people
should often *NOT* use ifelse().
In other words, I think ifelse() is much over-used in situations
where something else would be both clearer and more efficient.
Is there a document / book around which lures people into
misusing ifelse() so frequently?
Martin Maechler, ETH Zurich
jropers at freesurf> Peter Dalgaard wrote:
>> jropers at freesurf.fr wrote:
>>> What is puzzling me is that rnorm(1) is only evaluated
>>> *twice*, one time for each branch, with only 2 different
>>> random deviates, instead of giving ten different random
>>> deviates. y1 has indeed 10 values but with only 2
>>> different ones.
>>>
>> I find it more puzzling why you expect that ifelse, a
>> function of three vector arguments, would cause its input
>> arguments to be reevaluated for every element of the
>> result.
At 10:42 13/12/2006, Martin Maechler wrote:
"jropers at freesurf" == jropers at freesurf fr <jropers at freesurf.Fr>
on Tue, 12 Dec 2006 22:24:33 +0100 writes:
jropers at freesurf> ...ifelse, a function of three **vector**
jropers at freesurf> arguments.... Yes !! I misunderstood the
jropers at freesurf> functioning of ifelse.
Seems to happen more an more often.
When I teach "R programming" I nowadays usually emphasize that people
should often *NOT* use ifelse().
In other words, I think ifelse() is much over-used in situations
where something else would be both clearer and more efficient.
Is there a document / book around which lures people into
misusing ifelse() so frequently?
Perhaps it is because here two concepts are involved which may be less familiar to people: program control and vectorisation. I wonder whether the manual pages for ifelse and if need to do more than just See also the other one. I notice that the page for if has a very helpful health warning about putting braces round everything. This is unusual in the manual pages which usually describe what is rather than tell you explicitly what to do to achieve salvation. Perhaps I could suggest that the if page tells you what error message you get when you wanted ifelse and that the ifelse page has a health warning along the lines of 'ifelse is not if and they are often confused with difficult to understand results'. [snip] Michael Dewey http://www.aghmed.fsnet.co.uk