You can at least get rid of the

for (i in 1:200){
y[i]<-rbinom(1,1,0.8)
x1[i]<-ifelse(y[i]==1,rnorm(1,mean=20, sd=2),rnorm(1,mean=16, sd=2.2))
....

loop with the following

y <- rbinom(200, 1, 0.8)
y.1 <- y == 1   # get logical vector of y == 1
x1 <- numeric(200)  # allocate the vector
x1[y.1] <- rnorm(sum(y.1), 20, 2)
x1[!y.1] <- rnorm(sum(!y.1), 16, 2.2)

I don't know what else you are doing in the loops, but you should be
thinking "vectorized" when using R and avoid 'for' loops since they
are not the most efficient way of going things, especially if you are
going to be them hunreds of times.

On Thu, Jun 26, 2008 at 4:23 AM, sigalit mangut-leiba <[EMAIL PROTECTED]> wrote:
> Hi,
> I'm trying to do a double for loop like this:
> for (k in 1:1000){
> for (i in 1:200){
> y[i]<-rbinom(1,1,0.8)
> x1[i]<-ifelse(y[i]==1,rnorm(1,mean=20, sd=2),rnorm(1,mean=16, sd=2.2))
> ....
> }
> for (j in 1:300){
> ....
> }
> }
> Does anyone know a good reference about double loops?
> Thank you,
> Sigalit
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.

Reply via email to