Home > Mobile >  Using Expand.grid or crossing with multiple columns
Using Expand.grid or crossing with multiple columns

Time:06-18

I am trying to make a df that contains a row for every possible combination of multiple groups. For an example, we can stick to letters so I can show the issue in a reproducable way.

Let's say Group 1 consists of every combination of TWO selected letters a-e and Group 2 is simply just the letters f-i. Here's code to create each group:

group_1 <- data.frame(t(combn(letters[1:5], 2)))
group_2 = data.frame(t(combn(letters[6:9], 1)))

My best thought would be to use expand.grid somehow but maybe there's a better way. In the end, I believe there should be a df with 40 rows and three columns of all possible combos as the combination of 5 choose two (10) for group 1 * 4 for group 2 = 40.

CodePudding user response:

We can use crossing

library(tidyr)
names(group_2) <- 'X3'
crossing(group_1, group_2)

-output

# A tibble: 40 × 3
   X1    X2    X3   
   <chr> <chr> <chr>
 1 a     b     f    
 2 a     b     g    
 3 a     b     h    
 4 a     b     i    
 5 a     c     f    
 6 a     c     g    
 7 a     c     h    
 8 a     c     i    
 9 a     d     f    
10 a     d     g    
# … with 30 more rows

CodePudding user response:

Here is a base R approach:

names(group_2) <- 'X3'
df <- merge(group_1, group_2)
df[order(df[,1], df[,2]), ]
  X1 X2 X3
1   a  b  f
11  a  b  g
21  a  b  h
31  a  b  i
2   a  c  f
12  a  c  g
22  a  c  h
32  a  c  i
3   a  d  f
13  a  d  g
23  a  d  h
33  a  d  i
4   a  e  f
14  a  e  g
24  a  e  h
34  a  e  i
5   b  c  f
15  b  c  g
25  b  c  h
35  b  c  i
6   b  d  f
16  b  d  g
26  b  d  h
36  b  d  i
7   b  e  f
17  b  e  g
27  b  e  h
37  b  e  i
8   c  d  f
18  c  d  g
28  c  d  h
38  c  d  i
9   c  e  f
19  c  e  g
29  c  e  h
39  c  e  i
10  d  e  f
20  d  e  g
30  d  e  h
40  d  e  i
  • Related