tonykejrkkejrenrk
tonykejrkkejrenrk

Reputation:

What does rnorm in R return when the sd argument contains a vector?

What does the following code do:

rnorm(10, mean=2, sd=1:10)

The first number is from N(2,1)

The second number if from N(2,2)

The third number is from N(2,3)

etc...?

Upvotes: 4

Views: 4389

Answers (1)

gung - Reinstate Monica
gung - Reinstate Monica

Reputation: 11903

The first argument tells R how many random variates you want returned. In this case, it will give you back 10 values. Those values will be drawn from normal distributions with mean equal to 2. In addition, all 10 values will be drawn from distributions with different standard deviations, the first with SD=1, the second 2, ..., the 10th SD=10. Perhaps the thing to understand is that R, by its nature, is vectorized. That is, there is no such thing as a scalar, only a vector of length=1. (I recognize that that doesn't make a lot of sense within pure math, but it does in computer science.) As a result, arguments are often 'recycled' so that they will all match the length of the longest vector, i.e., you end up with a vector of 10 means, each equal to 2, to match your vector of 10 SDs. HTH.

Upvotes: 9

Related Questions