2

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Elvis
  • 405
  • 1
  • 4
  • 13

1 Answers1

2

You can do:

rbinom(10, 1, 0.4) + 1

Or use sample:

sample(1:2, 10, prob=c(0.6,0.4), replace=TRUE)
Lynn L
  • 523
  • 3
  • 7
StupidWolf
  • 45,075
  • 17
  • 40
  • 72