[R] matrix manipulations

Alberto Monteiro albmont at centroin.com.br
Wed Feb 28 19:00:29 CET 2007


Anup Menon Nandialath wrote:
> 
> I have a basic question with R. I'm generating a set
> of random variables and then combining them using the
> cbind statement. The code for that is given below.
> 
> for (i in 1:100)
>   {
>     y <- rpois(i,lambda=10)
>     X0 <- seq(1,1,length=i)
>     X1 <- rnorm(i,mean=5,sd=10)
>     X2 <- rnorm(i,mean=17,sd=12)
>     X3 <- rnorm(i,mean=3, sd=24)
>     ind <- rep(1:5,20)
>   }
> 
> data100 <- cbind(y,X0,X1,X2,X3,ind)
> 
First, why the loop? For i in 1:99, this code is a waste
of computer time. The code should be:

  i <- 100
  y <- rpois(i,lambda=10)
  X0 <- seq(1,1,length=i)
  X1 <- rnorm(i,mean=5,sd=10)
  X2 <- rnorm(i,mean=17,sd=12)
  X3 <- rnorm(i,mean=3, sd=24)
  ind <- rep(1:5,20)
  data100 <- cbind(y,X0,X1,X2,X3,ind)

> but when i look at the data100 table, the y values now
> take the observation count. 

The y values should be the same as y. y is a (random)
array of integers.

Alberto Monteiro



More information about the R-help mailing list