[R] Matrix output from drawing functions

Martin Maechler maechler at stat.math.ethz.ch
Fri Jan 14 17:48:31 CET 2000

>>>>> "Kjetil" == Kjetil Kjernsmo <kjetil.kjernsmo at astro.uio.no> writes:

    Kjetil> Dear all, I'm a bit confused about the output from functions
    Kjetil> that generates random values, e.g. rpois. I'm using 0.65.1 on
    Kjetil> Digital UNIX alphaev6.

    Kjetil> If I say
    >> rpois(10,5)
    Kjetil> output is not unexpectedly: [1] 4 6 5 7 6 5 2 2 5 3

    Kjetil> but I figured that if I go:
    >> rpois(10,1:10)
    Kjetil> I would get a 10x10 matrix as output, with random values for
    Kjetil> each of the vector elements in the row vectors, but I'm getting
    Kjetil> [1] 3 4 3 4 6 9 10 6 11 9 so, that is obviously not the
    Kjetil> case... :-) BTW, what am I getting...?
one for each lambda. 
There is argument "recycling"..
One could argue that R should issue a warning here, but
it *is* completely S compatible...

Maybe this shows better what happens:

 > round(rnorm(4, m=c(0,100), sd = c(1e-4,1,10)),2)
 [1]   0.00 100.77  10.80 100.00

 i.e. (m,s) are  (0,"0"), (100,1), (0,10), and (100,"0")


    Kjetil> So, I went on to thinking that
    >> outer(10, 1:10, rpois)
    Kjetil> would do the trick, but no.... Again, what am I getting?

This was almost correct:
    > outer(rep(10,10), 1:10, rpois)
would have done the job.
To see more clearly what happens, rather use

   outer( rep(10,8), 1+2*(0:9), rpois)

Note that AFAIK there are no (?) functions in R (or S) that inherently do
"outer".  The general principle rather is "recycling".

Regards: Martin
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list