I want to create a function that generates a random number based on its input and apply it to a boolean vector. This function will be used to generate test data with approx 500M elements.
f <- function(x, p) ifelse(x, runif(1)^p, runif(1)^(1/p))
f(c(T,T,T,F,F,F), 2)
What I get is not what I wanted.
[1] 0.0054 0.0054 0.0054 0.8278 0.8278 0.8278
I'd expect a new random number for every element of my input vector, not two random numbers repeated. Why do I get this result, and how can I get the desired result which would be the same as
c(runif(3)^2, runif(3)^(1/2))
which yields a new random number for every element
0.0774 0.7071 0.2184 0.8719 0.9990 0.8819
You would need to make two different vectors of random numbers of the same length as the
x
-vector.Or more generally:
The
ifelse
-function is not really doing looping. The second and third arguments get evaluated once each.@BondedDust's answer is correct (i.e.,
ifelse()
doesn't really loop) but slightly inefficient -- it samples twice as many random uniform deviates as necessary (in practice it wouldn't matter much unless you were using a huge vector or running the function huge numbers of times). Here's a slightly more efficient version which vectorizes over the power (^
) operator instead:@Frank points out in comments that
runif(length(x))^(p^(2*x-1))
is even better, although it's a little too clever for my taste.