Rel_Ai
Rel_Ai

Reputation: 591

How to find all the row combinations of two dataframes in R?

I am trying to return the combinations of all the possible rows of the following data frame for n times.

test <- expand.grid(rep(list(0:1),3))

For example, now the test is a data frame of 3 columns and 8 rows as follows:

  Var1 Var2 Var3
1    0    0    0
2    1    0    0
3    0    1    0
4    1    1    0
5    0    0    1
6    1    0    1
7    0    1    1
8    1    1    1

For example, combinations with n=2 would then provide a data frame of 6 columns and 64 rows. It is also acceptable if the result is in a list of 64 main elements where each element returns a combination of the two data frames.

I feel that I can still use expand.grid() but did not manage to use it correctly, I guess.

Upvotes: 0

Views: 106

Answers (1)

Rel_Ai
Rel_Ai

Reputation: 591

I have figured it out as soon as I posted the question.

I can just use the code of the test as follows:

expand.grid(rep(list(0:1),3*n))

Upvotes: 1

Related Questions