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 "However bad life may seem, there is always something you can do and succeed at. While there is life, there is hope." Stephen Hawking Anup Menon Nandialath ********************************************************* http://www.soundclick.com/bands/7/tailgunner_music.htm * ********************************************************* ____________________________________________________________________________________ Looking for earth-friendly autos? Browse Top Cars by "Green Rating" at Yahoo! Autos' Green Center.
Anup Nandialath wrote:> 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. > >That is not what is going on. Sounds like you have your column labels misaligned with the column contents. Take a look at m <- matrix(rnorm(4), 2, 2) m colnames(m) <- c("a", "b") m dim(m) (what was that for loop supposed to be good for, by the way?)
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
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