[R] Looping through all possible combinations of cases

Dimitri Liakhovitski ld7631 at gmail.com
Fri Jul 27 20:35:39 CEST 2007


Hello!

I have a regular data frame (DATA) with 10 people and 1 column
('variable'). Its cases are people with names ('a', 'b', 'c', 'd',
'e', 'f', etc.). I would like to write a function that would sum up
the values on 'variable' of all possible combinations of people, i.e.

1. I would like to write a loop - in such a way that it loops through
each possible pair of cases (i.e., ab, ac, ad, etc.) and sums up their
respective values on 'variable'

2. I would like to write a loop - in such a way that it loops through
each possible trio of cases (i.e., abc, abd, abe, etc.) and sums up
their respective values on 'variable'.

3.  I would like to write a loop - in such a way that it loops through
each possible quartet of cases (i.e., abcd, abce, abcf, etc.) and sums
up their respective values on 'variable'.

etc.

Then, at the end I want to capture all possible combinations that were
considered (i.e., what elements were combined in it) and get the value
of the sum for each combination.

How should I do it?
Thanks a lot!
Dimitri



More information about the R-help mailing list