Dear R-helpers, How come that in the following code the rnorm() function is evaluated only once for each branch of the 'ifelse' ? x <- rnorm(10) y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) What is the right way to make it called/evaluated for each row, apart from a 'for' loop ? Thanks, Jacques.
Dear R-helpers, How come that in the following code the rnorm() function is evaluated only once for each branch of the 'ifelse' ? x <- rnorm(10) y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) What is the right way to make it called/evaluated for each row, apart from a 'for' loop ? Thanks, Jacques.
ifelse is vectorized but there is no way you could know what's happening with that command because you have rnorm(1) for both conditions. I think you mean to have something different in one of them ? Whewn I run your code in my R session, I get 10 values for y1, so there isn't anything wrong except That you have the same statement for both cases. -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of jropers at freesurf.fr Sent: Tuesday, December 12, 2006 3:13 PM To: r-help at stat.math.ethz.ch Subject: [R] ifelse question Dear R-helpers, How come that in the following code the rnorm() function is evaluated only once for each branch of the 'ifelse' ? x <- rnorm(10) y1 <- ifelse(x > 0, rnorm(1) , rnorm(1)) What is the right way to make it called/evaluated for each row, apart from a 'for' loop ? 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. -------------------------------------------------------- This is not an offer (or solicitation of an offer) to buy/se...{{dropped}}
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.
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. >> > >
Martin Maechler
2006-Dec-13 10:42 UTC
[R] ifelse misusage become more and more frequent...
>>>>> "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.