Skip to content
Back to formatted view

Raw Message

Message-ID: <457F1AE8.3040809@gmail.com>
Date: 2006-12-12T21:11:04Z
From: Jacques Ropers
Subject: ifelse question
In-Reply-To: <148ed8180612121246q4c61a263oe8f4dd6052593575@mail.gmail.com>

> But you got only two (eventually one) distinct values, right? Look at
> the code for 'ifelse': yes and no are only called once each, then
> recycled to desired length.
>
> I guess you want something like
>
> x <- rnorm(10)
> y <- rnorm(10)
> z <- rnorm(10)
> y1 <- ifelse(x > 0, y, z)
>   
Thanks for the help.

Although this would do the trick, is there a way to call repetitively 
rnorm (rpois...) *inside the ifelse* rather than constructing the vector 
outside ? Like in the following where cos() and sin() functions are 
evaluated for each row :
x <- rnorm(10)
y1 <- ifelse(x > 0, cos(x), sin(x))

I am trying to understand the difference of behaviour. R acts as if 
rnorm(1) return value were known after the first call and does not 
evaluate rnorm(1) in

y1 <- ifelse(x > 0, rnorm(1) ,  rnorm(1))

again after the first evaluation.


Jacques.