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?
Asked
Active
Viewed 532 times
2
-
4you add 1? 1+ rbinom(10, 1, 0.4) ? or you can also do sample(1:2,probs=c(0.4,0.6),replace=TRUE). – StupidWolf Apr 14 '20 at 14:16
-
1@StupidWolf, please post that as an answer ... – Ben Bolker Apr 14 '20 at 14:17
1 Answers
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