Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this what rnorm(x) does if x is a vector, and how could I have found out faster?

Tags:

r

I’m looking for R resources, and I started looking at “An Introduction to R” here at r-project.org. I did and got stumped immediately.

I think I've figured out what’s going on, and my question is basically

  • Are there resources to help me figure out something like this more easily?

The preface of the Introduction to R suggests starting with the introductory session in Appendix A, and right at the start is this code and remark.

x <- rnorm(50)
y <- rnorm(x)

Generate two pseudo-random normal vectors of x- and y-coordinates.

The documentation says the (first and only non-optional) parameter to rnorm is the length of the result vector. So x <- rnorm(50) produces a vector of 50 random values from a normal distribution with mean 0 and standard deviation 1.

So far so good. But why does rnorm(x) seem to do what y <- rnorm(50) or y <- rnorm(length(x)) would have done? Either of these alternatives seem clearer to me.

My guess as to what happens is this:

  1. The wrapper for rnorm didn’t care what kind of thing x is and just passed to the underlying C function a pointer to the C struct for x as an R object.

  2. R objects represented in C are structs followed by “data”; the data of the C representation of an R vector of reals starts with two integers, the first of which is the vector's length. (The vector elements follow those integers.) I found this out by reading up on R internals here.

  3. If a C function were written to find the value of an R integer from a passed pointer-to-R-object, and it were called with a pointer to an R vector of reals, it would find the vector’s length in the place it would look for the single integer.

In addition to my main question of “How can I figure out something like this more easily?”, I wouldn’t mind knowing whether what I think is going on is correct and whether the fact that rnorm(x) is idiomatic R in this context or more of a sloppy choice. Given that it does something useful, can it be relied upon or is it just lucky behavior for an expression that isn’t well-defined in R?

I’m used to strongly-typed languages like C or SQL, which have easier-to-follow (for me) semantics and which also have more comprehensive references available, so any references for R that have a programming-language-theory focus or are aimed at people used to strong typing would be good, too.

like image 273
Steve Kass Avatar asked May 18 '18 21:05

Steve Kass


People also ask

Is Rnorm a vector?

rnorm generates a vector of normally distributed random numbers.

What does Rnorm mean?

rnorm is the R function that simulates random variates having a specified normal distribution. As with pnorm , qnorm , and dnorm , optional arguments specify the mean and standard deviation of the distribution.

How does Rnorm work in R?

The rnorm() function in R generates a random number using a normal(bell curve) distribution. Thus, the rnorm() function simulates random variates having a specified normal distribution.

Can a vector have one value in r?

In R, a single element is always a vector of length 1, there is no special object for single values.


1 Answers

It is documented behavior. From ?rnorm:

Usage: [...]

 rnorm(n, mean = 0, sd = 1)

Arguments: [...]

   n: number of observations. If ‘length(n) > 1’, the length is
      taken to be the number required.
like image 65
Ralf Stubner Avatar answered Sep 22 '22 06:09

Ralf Stubner