简体   繁体   中英

Attempting to generate a list in R using replicate

I'm working through exercises in DataCamp through a course I'm currently taking on EdX.

The specific question I'm stuck on requires me to create a list (l) of all possible outcomes in remaining games.

The number of remaining games is:

n <- 6

I have my simple vector outcomes here which has a 0 for a loss and a 1 for a win.

outcomes <- c(0,1)

Now, the specific part of the question I'm struggling with is: "Assign a variable l to a list of all possible outcomes in all remaining games. Use the rep function to create a list of n games, where each game consists of list(outcomes)"

Here's my code:

l <- replicate(n, sample(outcomes, n, replace=TRUE))

Now, I think my biggest problem here is that I'm only generating n amount of games when I should be generating the total number of games possible. I'm not exactly sure how to do this and even after looking over documentation, I've been stuck for awhile.

Thanks for any and all help. Also, is this proper etiquette for this type of question?

We can get all possible outcomes by using a list of outcomes with rep() and expand.grid() :

n = 6
outcomes = c(0,1)
l = rep(list(outcomes), n)
> expand.grid(l)
   Var1 Var2 Var3 Var4 Var5 Var6
1     0    0    0    0    0    0
2     1    0    0    0    0    0
...
63    0    1    1    1    1    1
64    1    1    1    1    1    1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM