0

I am interested in generating the input for the example mentioned in this question. I have generated the input using the below snippet

data.frame(grade = rep(LETTERS[1:5],4,rep=TRUE), 
       sex = c( rep(c('male','female'), each=5), rep('male',4),rep('female', each=6)),
       class=rep(c(1,2),each=10))

It gives the below output:

   grade    sex class
1      A   male     1
2      B   male     1
3      C   male     1
4      D   male     1
5      E   male     1
6      A female     1
7      B female     1
8      C female     1
9      D female     1
10     E female     1
11     A   male     2
12     B   male     2
13     C   male     2
14     D   male     2
15     E female     2
16     A female     2
17     B female     2
18     C female     2
19     D female     2
20     E female     2

I am not sure if this is the best possible way of generating this output. Would like to know if there is any smart of doing this.

If this is not acceptable as a question, please suggest the correct way of asking it.

Community
  • 1
  • 1
Prradep
  • 5,506
  • 5
  • 43
  • 84
  • 1
    This data doesn't have a robust structure (because of row 15) so I don't think there is some magic way to create it programmatically. If `sex == male` at row 15, you could use @Pascals `expand.grid` solution. Or you can just override line 15 manually after using it. – David Arenburg Jun 22 '15 at 10:29
  • @DavidArenburg This is not having a pattern and with a difference in sex in row 15 as you mentioned. I couldn't find any solution here, which one you are referring to ? – Prradep Jun 22 '15 at 10:36
  • This was posted and deleted by @Pascal `expand.grid(grade = LETTERS[1:5], sex = c('male','female'), class = 1:2)` You could also use `CJ` from `data.table`. – David Arenburg Jun 22 '15 at 10:37
  • @DavidArenburg means e.g. `data.table::CJ(letters[1:5],c("Male","Female"),1:2)` – Simon O'Hanlon Jun 22 '15 at 10:48

0 Answers0