samedi 8 septembre 2018

How to make my existing R codes output a specific count of odd and even numbers?

I have the following codes running in R. It basically outputs 6 random numbers based on my choice of the frequency of numbers from each list.

foo <- list(
  c(1,2,3,4,5,6,7,8,9,10), 
  c(11,12,13,14,15,16,17,18,19,20),
  c(21,22,23,24,25,26,27,28,29,30), 
  c(31,32,33,34,35,36,37,38,39,40) 
)

names(foo) <- c(3, 2, 1, 0)

list2<- unlist(sapply(seq_along(foo), function(x) sample(foo[[x]], names(foo[x]))))

sort(list2)

So, above codes will output 6 random numbers (3 from 1st list, 2 from 2nd list, 1 from 3rd list and none from 4th list).

I want to also be able to add to these codes the choice of selecting the number of odd numbers and even numbers in the final output.

Let's say I want to keep the existing codes but I want the output to be a mix of 4 even numbers and 2 odd numbers.

How can I do this?




Aucun commentaire:

Enregistrer un commentaire