user81411
user81411

Reputation: 457

How does prob argument in rbinom work when prob is a vector?

Consider the following code:

rbinom(17, 1, .5)

This code denotes each observation has probability of success equal to 0.5.

In rbinom(3, 1, c(.5,.3,.7)), the first observation has probability of success 0.5, the second observation has probability of success equal to 0.3, and the last observation has probability of success equal to 0.7.

But in

rbinom(17, 1, c(.5,.3,.7))

, how is the probability of success distributed among the 17 observation?

Upvotes: 6

Views: 3614

Answers (1)

Spacedman
Spacedman

Reputation: 94237

The vector is recycled over the 17 generated values:

> rbinom(17, 1, c(0,.999))
 [1] 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

Often R will generate a warning if you try recycling two vectors that aren't don't fit into each other:

> (1:10) + (1:3)
 [1]  2  4  6  5  7  9  8 10 12 11
Warning message:
In (1:10) + (1:3) :
  longer object length is not a multiple of shorter object length

but not in this case.

Upvotes: 8

Related Questions