I have fallowing data frame:
> my.df
x y
1 0.4597406 0.8439140
2 0.4579697 0.7461805
3 0.5593259 0.6646701
4 0.3607346 0.7792931
5 0.8377520 1.0445919
6 0.5597406 1.0445919
I want to create all possible combinations
> my.df
x y
1 0.4597406 0.8439140
2 0.4597406 0.7461805
3 0.4597406 0.6646701
4 0.4597406 0.7792931
5 0.4597406 1.0445919
6 0.4597406 1.0445919
7 0.4579697 0.8439140
8 0.4579697 0.7461805
9 0.4579697 0.6646701
...
(Not all the combinations are showing here - This is to show the format that I would like to get the resulting data frame)
Using following functions didn't really give the exact combinations.
expand.grid(my.df)
Whats the best way to generate all possible combinations.
A
Cross Join
is helpful in this situation. Since you didnt provide a reproducible example. I have create my own datset.expand.grid() will give you all the possible combinations but not the unique combinations. If you need the latter you can use a function like this
Or as JTT points that this can be done in one line with
I know everyone's throwing
expand.grid()
at you, so here's another option...tidyr
has acomplete()
function which "completes" your data combinations, which I believe is what you're after.Note: this produces the unique combinations - your expected output rows 5 and 6 are identical.
We can just use
expand.grid
Or with
data.table
Maybe we can use
expand.grid
in the following wayYou could use the merge function this way