samedi 2 février 2019

How do I change my existing R codes from the output of a random sample to a list of all possible outcomes?

I have the following R codes running in RStudio:

foo <- list(
c(1,2,3,5,6,7,8,9), 
c(11,13,15,16,17,19), 
c(21,25,28,29), 
c(31,33,34,35,37,38,40) 
)

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

is.odd <- function(x) x %% 2 != 0
countOdds<-function(dt){ sum(sapply(dt, is.odd)) }

while(T){
list2<- unlist(sapply(seq_along(foo), function(x) sample(foo[[x]], names(foo[x]))))
if(countOdds(list2)==2)
break
         }

What the actual codes do:

The output is a random output (1 single outcome) from all possible outcomes.

Example: 2  6 16 25 28 37

In this output, we have 2 ODD numbers and 4 EVEN numbers as specified in the last part of the codes.

What I want to achieve:

I need to modify the codes so that it gives me ALL the possible outcomes from what has been specified instead of a single random output. Ideally, I would like to write this new output into a CSV file.

Say, something like:

write.csv(result1, file = "result1.csv")

How can I do this?




Aucun commentaire:

Enregistrer un commentaire