[R] vector argument to rnorm

Prof Brian Ripley ripley at stats.ox.ac.uk
Sat Mar 3 09:21:27 CET 2001


On Sat, 3 Mar 2001, John Aitchison wrote:

>
> Is this intended behaviour ?
>
> > length(rnorm(c(100)))
> [1] 100
> > length(rnorm(c(100,0,1)))
> [1] 3
> > length(rnorm(length(c(100,1,2))))
> [1] 3
> > length(rnorm(c(100,0,1,2,3,4,5)))
> [1] 7
>
> ie if you pass in a single element vector the first element of that is
> taken as the desired n (OK) , but otherwise the length of the vector
> argument is taken as the desired n.

[Yes: see below.]

> I came across this usage in an example for terasvirta.test (same for
> white.test) in tseries
>
> > example(terasvirta.test)
>
> trsvr.> n <- 1000
> trsvr.> x <- runif(1000, -1, 1)
> trsvr.> y <- x^2 - x^3 + 0.1 * rnorm(x)
> trsvr.> terasvirta.test(x, y)
>
> and it seemed to me that rnorm(n) was what was intended, not that it would
> make any difference in practice in this case.
>
> Is this a standard practice? .. that in passing in a vector where a scalar
> is expected it is as if you passed in the length of that vector as the
> argument (unless the vector has length 1)

Yes, for sample sizes in rxxxx.  It is what S does:

n:      sample  size.   If  length(n)  is  larger  than  1,  then
       length(n) random values are returned.

Yet another case where R's help pages do not tell the whole story.
I will try to fix these.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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