rrandombinomial-cdf

How to generate random number 1 & 2 instead of 0 & 1 from binomial distribution?


I would like to generate random number from binomial 1 or 2 instead of the usual 0 and 1. Normally we will use rbinom(10, 1, 0.4) to generate either 0 or 1, but is it possible to generate 1 and 2? Or is there any way to convert it?


Solution

  • You can do:

    rbinom(10, 1, 0.4) + 1
    

    Or use sample:

    sample(1:2, 10, prob=c(0.6,0.4), replace=TRUE)