[R] matrix manipulations

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Feb 28 18:57:34 CET 2007


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?)



More information about the R-help mailing list