[R] Getting all possible combinations
peter dalgaard
pdalgd at gmail.com
Wed Aug 23 22:58:31 CEST 2017
> On 23 Aug 2017, at 20:51 , Ista Zahn <istazahn at gmail.com> wrote:
>
> On Wed, Aug 23, 2017 at 12:35 PM, Bert Gunter <bgunter.4567 at gmail.com> wrote:
>> ummm, Ista, it's 2^n.
>
> ummm yes ughhhh.
>
You didn't really say otherwise: sum(choose(n,0:n)) == 2^n by the binomial expansion of (1+1)^n (but you knew that)
This points to a different algorithm where you write 0:(2^n-1) as n-digit binary numbers and chose items corresponding to the 1s. That won't give the combinations sorted by size of selected subgroup though. Something like this:
M <- as.matrix(do.call(expand.grid, rep(list(0:1),5)))
mode(M) <- "logical"
apply(M,1,function(i)LETTERS[1:5][i])
-pd
> My point is, if the number of groups is large, check it before hand.
> If you can check it without embarrassing yourself in public like I did
> that's even better.
>
> Best,
> Ista
>
>>
>> Cheers,
>> Bert
>>
>>
>> Bert Gunter
>>
>> "The trouble with having an open mind is that people keep coming along
>> and sticking things into it."
>> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>>
>>
>> On Wed, Aug 23, 2017 at 8:52 AM, Ista Zahn <istazahn at gmail.com> wrote:
>>> On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
>>> <bogaso.christofer at gmail.com> wrote:
>>>> Hi again,
>>>>
>>>> I am exploring if R can help me to get all possible combinations of
>>>> members in a group.
>>>>
>>>> Let say I have a group with 5 members : A, B, C, D, E
>>>>
>>>> Now I want to generate all possible unique combinations with all
>>>> possible lengths from that group e.g.
>>>>
>>>> 1st combination : A
>>>> 2nd combination : B
>>>> .....
>>>> 5th combination : E
>>>> 6th combination : A, B
>>>> 7th combination : B, C
>>>> ....
>>>> last combination: A, B, C, D, E
>>>>
>>>> Ideally, I have a fairly large group so am looking for some
>>>> programmatic way to generate all possible combinations.
>>>
>>> Be careful, the number of combinations grows pretty quickly. You can
>>> calculate the number ahead of time with
>>>
>>> sum(choose(n, 1:n))
>>>
>>> where n is the number of values in your group.
>>>
>>> --Ista
>>>
>>>>
>>>> Any help will be highly appreciated.
>>>>
>>>> Thanks for your time.
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
More information about the R-help
mailing list