Skip to content
Prev 44795 / 398506 Next

matrix() Help

On Tue, 2004-02-24 at 10:51, Jonathan Wang wrote:
See the help for rnorm(), which says:

n: number of observations. If 'length(n) > 1', the length is
          taken to be the number required.

Thus, by using rnorm(matrix1), you get 3 numbers back, which is the
length of matrix1. Since 3 is less than 24 (3 x 8), the vector from
rnorm is cycled to fill in the matrix.

Use lapply() and have the final result be a 'list', since the number of
values in each component will vary:

matrix1 <- matrix(rpois(3, lambda = 2), nrow = 3, ncol = 1)
[,1]
[1,]    5
[2,]    3
[3,]    1

list2 <- lapply(matrix1, rnorm)
[[1]]
[1] -1.186458639  1.096777044 -0.005344028  0.707310667  1.034107735

[[2]]
[1]  0.2234804 -0.8787076  1.1629646

[[3]]
[1] -2.000165

See ?lapply

Watch out for situations where an element in matrix1 is a zero. You will
get a numeric(0) as a result in that list element.

HTH,

Marc Schwartz