1

I would like to generate a random sample of size 80 whose elements are either 1 or 2. I want 70% 1 and 30% 2. How can I do that in R? Any help is appreciated.

Uddin
  • 754
  • 5
  • 18

2 Answers2

4

We can use sample by specifying the prob

sample(1:2, 80, prob = c(.7, .3), replace = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662
4

If you need a deterministic ratio instead of a probabilistic one, then

sample(c(rep(1, 56), rep(2, 24)))
# or
sample(rep(1:2, times=c(56, 24)))
r2evans
  • 141,215
  • 6
  • 77
  • 149