I'm attempting to independently select elements of an array that meet a condition and then convert those elements to a different value, with a given frequency. In R I'd use rbinom, but can't find the correct function in Julia. MWE:
#create array
a = [3,4,3,6,3];
# convert elements that match ==3 to 9
a[a .==3] .=9;
a
[9,4,9,6,9]
but what I can't figure out how to do is the conversion with binomial probability (might be bernoulli?) of, for example, 0.66 so converted a
might end up as
[3,4,9,6,9]
I think what I want is something like:
a[a .==3] .= bernoulli(0.66)9
Such that the probability of being converted from 3 to 9 is 0.66. So 66% of the time it will convert the 3 to 9, and the remaining 33% it will just leave it as a 3. Make sense?
I'd like to do the evaluation independently on each instance of 3, rather than extracting an index vector of all the 3's and then calculating how many are converted with a probability (if that makes any sense?!) Thx J
rbinom
in R, it doesn't look like it has anything to do with replacements in vectors, only with generating random values. If you want something like that, look at the Distributions.jl package, and you can find something relevant. My code does not replicate the functionality ofrbinom
, but of the replacement functionality you described in your post. – DNF