I might try something like: data[data$flag1 == 1, "flag2"] <- runif(sum(data$flag1 == 1)) < 0.95
and similarly for the other case. Hope this helps, Michael On Mon, Jun 18, 2012 at 12:29 PM, nqf <n.j.frank...@pgr.reading.ac.uk> wrote: > Dear R-help, > > I am trying to write a function to simulate datasets of size n which contain > two time-to-event outcome variables with associated 'Event'/'Censored' > indicator variables (flag1 and flag2 respectively). One of these indicator > variables needs to be dependent on the other, so I am creating the first and > trying to use this to create the second using an if/else statement. > > My data structure needs to follow this algorithm (for each row of the data): > If flag1=1 then flag2 should be 1 with probability 0.95 and zero otherwise > Else if flag1=0 then flag2 should be 1 with probability 0.5 and zero > otherwise > > I can set up this example quite simply using if else statements, but this is > incredibly inefficient when running thousands of datasets: > data<-as.data.frame(rbinom(10,1,0.5)) > colnames(data)<-'flag1' > for (i in 1:n) { > if (data$flag1[i]==1) {data$flag2[i]<-rbinom(1,1,0.95)} else > {data$flag2[i]<-rbinom(1,1,0.5)} > } > > > I think to speed up the simulations I would be better changing to > vectorisation and using something like: > ifelse(data$flag1==1,rbinom(1,1,0.95),rbinom(1,1,0.5)) > but the rbinom statements here generate one value and repeat this draw for > every element of flag2 that matches the 'if' statement on flag1. > > Is there a way to assign flag2 to a new bernoulli draw for each subject in > the data frame with flag1=1? > > I hope my question is clear, and thank you in advance for your help. > > Thanks, > Natalie > PhD student, Reading University > > P.S. I am using R 2.12.1 on Windows 7. > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Trying-to-speed-up-an-if-else-statement-in-simulations-tp4633725.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@r-project.org 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. ______________________________________________ R-help@r-project.org 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.