Im writing some code in R whereby i need to generate a data set from specific criteria given they meet certain conditions.
I have three probabilities
A – 0.423, B – 0.324 and C- 0.253.
I want to run a random generated sample runif(50, 0, 1).
If the number generated lies between 0 and 0.423, i want to generate a value from rnorm(50, 25, 4),
if its between 0.423 and 0.747, i want to generate a value from rnorm(50, 28, 4.5)
and finally if its between 0.747 and 1, i want to generate a value from rnorm(50, 30, 5).
I was trying to do this using some sort of compound ifelse function but to no avail.
Any suggestions?
Cheers
As you suspected, this can be done using
ifelse:This works because
ifelsecan operate along vectors.You might prefer this method, especially if you end up having more than three sections to break it into: