[R] matrix() Help

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Feb 24 19:09:43 CET 2004


Jonathan Wang wrote:

> I have a question related to matrix().
> 
> The code below randomly generates 3 Poisson numbers into a 3 by 1 matrix:
> 
> 
>>matrix1 <- matrix(rpois(3,lambda=2),nrow=3,ncol=1)
> 
> 
> And I use list() to see what they are:
> 
>      [,1]
> 
> [1,]  3
> 
> [2,]  1
> 
> [3,]  4
> 
> , which is what I had intended.


For sure you used print(), but not list() ...



> I then I want to randomly generate y Normal numbers into a 3 by 8 matrix. y in this case would be 3, 1, and 4; so the resulting matrix should be such that the first row has three different Normal numbers; the second row has 1 Normal number; and the third row has four different Normal numbers. I have the following code to do that:
> 
> 
>>matrix2 <- matrix(rnorm(poisNums),nrow=3,ncol=8)
> 
> 
> To my surprise, matrix2 does not look anything like what I had intended. Instead, it repeats the values in the first column (of the matrix2) eight times. What part of the code do I need to fix?

You don't want to use a matrix here, but a list:

list2 <- apply(matrix1, 1, rnorm)

Please read "An Introduction to R" and learn a bit more on data 
structures in R ...

Uwe Ligges




> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list