[R] matrix manipulations

Petr Klasterecky klaster at karlin.mff.cuni.cz
Wed Feb 28 19:16:16 CET 2007


Don't know what's wrong - it works:

 > data100
         y X0           X1           X2          X3 ind
   [1,] 11  1   2.79581511 -23.33335477 -33.6123061   1
   [2,]  8  1  21.43289242  21.52826214   3.8415209   2
   [3,]  6  1   6.18688631  21.51057247 -50.5547410   3
   [4,] 12  1  -5.95172686  13.74167916 -16.1798745   4
  <snip>

 > data100[y]
   [1]  9 10  8 18 10 10  9  7 11 11  7 10  9 14  7 18  9  7 10 10 18 10 
  9 11  9 10 11 11 11 10  8  9  8  7 18  9 11 15  7
  [40] 10  7  9  7 18 11 11 18 11 11 18  7 10 11  7 11 10 18  7  9  9  9 
  7  7  7 14 18 18 14 11 12 11  8 15  7  7  9 18 14
  [79] 10  7  7 12  7  7  9  8  7  8 11  7 15 18 18  7 15  7  7 11 11 10

These can pretty well be Poisson(10) variates... Remind that R is 
case-sensitive in names, y and Y is not the same.

However, I really hope you are not using the code given below since it 
is, well... very original... to generate something 100 times and to keep 
just the last value at the end.

Petr

Anup Nandialath napsal(a):
> Dear friends,
> 
> 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)
> 
> but when i look at the data100 table, the y values now
> take the observation count. (ie) the data under Y is
> not the poisson random generates but the observation
> number. Hence the last vector (ind) does not have a
> header. Is there any way i can drop the number of
> observation counts being added into the matrix.
> 
> Thanks in advance for your help.
> 
> Sincerely
> 
> Anup

-- 
Petr Klasterecky
Dept. of Probability and Statistics
Charles University in Prague
Czech Republic



More information about the R-help mailing list