# [R] how do I extract all possible combinations of rows from a column

arun smartpink111 at yahoo.com
Tue Jan 21 21:57:49 CET 2014

```Hi,
May be this helps:
library(gtools)

as.data.frame(t(combinations(5,3,1:5)))
#  V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
#1  1  1  1  1  1  1  2  2  2   3
#2  2  2  2  3  3  4  3  3  4   4
#3  3  4  5  4  5  5  4  5  5   5
A.K.

For a column of data, I need to extract all possible unique
combinations of 3 rows and combine the results into a data frame (as
columns). So, for example, if I had a column called "test" that had five rows (the contents of which were the numbers 1:5) all possible
combinations of 3 rows would be
1,2,3
1,2,4
1,2,5
1,3,4
1,3,5
1,4,5
2,3,4
2,4,5
2,4,5
3,4,5

So for this example, R would extract all the combinations shown, and I would get a data frame that looked like this
1     1     1     1     1     1     2     2     2      3
2     2     2     3     3     4     3     3     4      4
3     4     5     4     5     5     4     5     5      5

Note: I am not considering rearrangements of the same three rows
to be unique. In other words, the combinations 123, 132, 213, 231, 312,
321 are all the same as far as I am concerned. If it is not possible to
do it that way, it will still be helpful, but not quite as helpful.
Thanks for the help!

```