[R] Question about avoid the for loop

From: Carrie Li <carrieandstat_at_gmail.com>
Date: Thu, 03 Jun 2010 11:49:44 -0400


Dear R-helpers,

I would like to generate a binary random variable within a stratum's stratum. Here is a simple example.

## x is the first level strata index, here I have 3 strata.
x=c(rep(1,5), rep(2,5), rep(3,5))

## within x, there is a second strata indexed by t=0 and t=1
t=rep(c(0,0,1,1,1),3)

## and within strata i and t=0 and t=1, I generate the random binomial
variable respectively, and save in y
y=rep(NA, length(x))
for (i in 1:3)
{
y[(x==i)&(t==0)]=rbinom(2, 1, 0.2)
y[(x==i)&(t==1)]=rbinom(3, 1, 0.8)
}

My question: is there any way to avoid the for loop, since I have the first level strata has thousands of strata. (Within each x stratum, the t only has 2 levels, 0 and 1 )

Thanks for any help!

Carrie

        [[alternative HTML version deleted]]



R-help_at_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. Received on Thu 03 Jun 2010 - 16:00:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 03 Jun 2010 - 16:30:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive