Choose a subsample of random numbers

I will play in the Brazilian Lottery with my friends. I requested every one of them to choose seven numbers. I create a variable for all of them.

pestana = c(04, 15, 29, 36, 54, 25, 07)
carol = c(7, 22, 30, 35, 44, 51, 57)
davi = c(8, 13, 21, 29, 37, 42, 55)
valerio = c(30, 20, 33, 14, 7, 41, 54)
victor = c(09, 11, 26, 33, 38, 52, 57)

Then, I created a list with all of the numbers, and a list with unique numbers (in order to avoid repeated numbers)

list = c(carol, davi, pestana, valerio, victor, diuli, cynara)
list2 = unique(list)

Finally, I made a sample() for the list2

sample(list2, 7)

After that, I was wondering. Is it possible for me not to use the unique and not have repeated numbers? Because for instance, that way, repeated numbers have the same probability of appearing, when in fact, they have more (for instance, seven appeared three times).

Upvotes: 1

Views: 27

Answers (1)

DaveArmstrong
DaveArmstrong

Reputation: 21757

How about this:

pestana = c(04, 15, 29, 36, 54, 25, 07)
carol = c(7, 22, 30, 35, 44, 51, 57)
davi = c(8, 13, 21, 29, 37, 42, 55)
valerio = c(30, 20, 33, 14, 7, 41, 54)
victor = c(09, 11, 26, 33, 38, 52, 57)

list = c(carol, davi, pestana, valerio, victor)
l <- c(unlist(list))
nums <- table(l)
probs <- nums/sum(nums)

sample(names(probs), 7, prob = probs, replace=FALSE)
#> [1] "4"  "33" "44" "11" "29" "52" "8"

Created on 2022-12-14 by the reprex package (v2.0.1)

Using the prob argument, you can make some values more likely to show up than others.

Upvotes: 2

Related Questions