Getting all possible combinations

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting all possible combinations

Bogaso
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.

Any help will be highly appreciated.

Thanks for your time.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Ista Zahn
lapply(1:5, function(x) combn(groups, x))

or perhaps

unlist(lapply(1:5, function(x) combn(groups, x, FUN = paste, collapse = ", ")))

Best,
Ista

On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
<[hidden email]> 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.
>
> Any help will be highly appreciated.
>
> Thanks for your time.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Bert Gunter-2
In reply to this post by Bogaso
Please at least do a basic internet search before posting here.
Searching on "R combinations" brought up the combn() function, which
you can use in a simple loop to get your answer:

> lapply(seq_len(5), FUN = function(x)combn(LETTERS[1:5], x))
## You can use a for() loop if you prefer

[[1]]
     [,1] [,2] [,3] [,4] [,5]
[1,] "A"  "B"  "C"  "D"  "E"

[[2]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A"  "A"  "A"  "A"  "B"  "B"  "B"  "C"  "C"  "D"
[2,] "B"  "C"  "D"  "E"  "C"  "D"  "E"  "D"  "E"  "E"

[[3]]
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "A"  "A"  "A"  "A"  "A"  "A"  "B"  "B"  "B"  "C"
[2,] "B"  "B"  "B"  "C"  "C"  "D"  "C"  "C"  "D"  "D"
[3,] "C"  "D"  "E"  "D"  "E"  "E"  "D"  "E"  "E"  "E"

[[4]]
     [,1] [,2] [,3] [,4] [,5]
[1,] "A"  "A"  "A"  "A"  "B"
[2,] "B"  "B"  "B"  "C"  "C"
[3,] "C"  "C"  "D"  "D"  "D"
[4,] "D"  "E"  "E"  "E"  "E"

[[5]]
     [,1]
[1,] "A"
[2,] "B"
[3,] "C"
[4,] "D"
[5,] "E"



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:33 AM, Christofer Bogaso
<[hidden email]> 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.
>
> Any help will be highly appreciated.
>
> Thanks for your time.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Ista Zahn
In reply to this post by Bogaso
On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
<[hidden email]> 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Bert Gunter-2
ummm, Ista, it's 2^n.

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 <[hidden email]> wrote:

> On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
> <[hidden email]> 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.
>>
>> ______________________________________________
>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Spencer Graves-4


On 2017-08-23 11:35 AM, Bert Gunter wrote:
> ummm, Ista, it's 2^n.


       or (2^n-1) if the empty set is not considered as a "combination"
;-)  spencer

>
> 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 <[hidden email]> wrote:
>> On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
>> <[hidden email]> 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>> ______________________________________________
>> [hidden email] 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.
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Ista Zahn
In reply to this post by Bert Gunter-2
On Wed, Aug 23, 2017 at 12:35 PM, Bert Gunter <[hidden email]> wrote:
> ummm, Ista, it's 2^n.

ummm yes ughhhh.

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 <[hidden email]> wrote:
>> On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
>> <[hidden email]> 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Peter Dalgaard-2

> On 23 Aug 2017, at 20:51 , Ista Zahn <[hidden email]> wrote:
>
> On Wed, Aug 23, 2017 at 12:35 PM, Bert Gunter <[hidden email]> 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 <[hidden email]> wrote:
>>> On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
>>> <[hidden email]> 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.
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>>
>>> ______________________________________________
>>> [hidden email] 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.
>
> ______________________________________________
> [hidden email] 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: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Bert Gunter-2
Inline.

-- 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 1:58 PM, peter dalgaard <[hidden email]> wrote:

>
>> On 23 Aug 2017, at 20:51 , Ista Zahn <[hidden email]> wrote:
>>
>> On Wed, Aug 23, 2017 at 12:35 PM, Bert Gunter <[hidden email]> 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:

No it doesn't.
-- Bert

>
> 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 <[hidden email]> wrote:
>>>> On Wed, Aug 23, 2017 at 11:33 AM, Christofer Bogaso
>>>> <[hidden email]> 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.
>>>>>
>>>>> ______________________________________________
>>>>> [hidden email] 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.
>>>>
>>>> ______________________________________________
>>>> [hidden email] 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.
>>
>> ______________________________________________
>> [hidden email] 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: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Peter Dalgaard-2

> On 23 Aug 2017, at 23:12 , Bert Gunter <[hidden email]> wrote:
>
>> 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:
>
> No it doesn't.
> -- Bert

Doesn't what? Do what I say it won't??

-pd

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Bert Gunter-2
Doesn't sort by size of subgroup. I interpret the phrase I asterisked as:

Your code does the following:

First subsets of size 1 are given.
Then all subsets of size 2.
Then all subsets of size 3.
etc.

Your code does not do this (quite).

If you meant something else, then please clarify.

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 3:14 PM, peter dalgaard <[hidden email]> wrote:

>
>> On 23 Aug 2017, at 23:12 , Bert Gunter <[hidden email]> wrote:
>>
>>> 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:
>>
>> No it doesn't.
>> -- Bert
>
> Doesn't what? Do what I say it won't??
>
> -pd
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: [hidden email]  Priv: [hidden email]
>
>
>
>
>
>
>
>
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Duncan Murdoch-2
On 23/08/2017 6:25 PM, Bert Gunter wrote:
> Doesn't sort by size of subgroup. I interpret the phrase I asterisked as:

You were fooled by Peter's tricky single negative.

Duncan Murdoch

>
> Your code does the following:
>
> First subsets of size 1 are given.
> Then all subsets of size 2.
> Then all subsets of size 3.
> etc.
>
> Your code does not do this (quite).
>
> If you meant something else, then please clarify.
>
> 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 3:14 PM, peter dalgaard <[hidden email]> wrote:
>>
>>> On 23 Aug 2017, at 23:12 , Bert Gunter <[hidden email]> wrote:
>>>
>>>> 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:
>>>
>>> No it doesn't.
>>> -- Bert
>>
>> Doesn't what? Do what I say it won't??
>>
>> -pd
>>
>> --
>> Peter Dalgaard, Professor,
>> Center for Statistics, Copenhagen Business School
>> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
>> Phone: (+45)38153501
>> Office: A 4.23
>> Email: [hidden email]  Priv: [hidden email]
>>
>>
>>
>>
>>
>>
>>
>>
>>
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Peter Dalgaard-2

> On 24 Aug 2017, at 01:25 , Duncan Murdoch <[hidden email]> wrote:
>
> On 23/08/2017 6:25 PM, Bert Gunter wrote:
>> Doesn't sort by size of subgroup. I interpret the phrase I asterisked as:
>
> You were fooled by Peter's tricky single negative.
>

<giggles>...

Let's do this more carefully, then:

M <- as.matrix(do.call(expand.grid, rep(list(0:1),5)))
M
# This is just 0:31 encoded as (little-endian) binary
apply(M, 1, function(i) sum((2^(0:4))[i]))

# now, use rows of M for logical indexing into "A"-"E"
mode(M) <- "logical"
(l <- apply(M,1,function(i)LETTERS[1:5][i]))

# Clearly not sorted by group size:
#[[1]]
#character(0)
#
#[[2]]
#[1] "A"
#
#[[3]]
#[1] "B"
#
#[[4]]
#[1] "A" "B"
#
#[[5]]
#[1] "C"

# requires this to sort:

o <- order(sapply(l, length))
l[o]





--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Peter Dalgaard-2

> On 24 Aug 2017, at 11:58 , peter dalgaard <[hidden email]> wrote:
>
> apply(M, 1, function(i) sum((2^(0:4))[i]))

--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting all possible combinations

Peter Dalgaard-2
In reply to this post by Peter Dalgaard-2

> On 24 Aug 2017, at 11:58 , peter dalgaard <[hidden email]> wrote:
>
> M <- as.matrix(do.call(expand.grid, rep(list(0:1),5)))
> M
> # This is just 0:31 encoded as (little-endian) binary
> apply(M, 1, function(i) sum((2^(0:4))[i]))
>
> # now, use rows of M for logical indexing into "A"-"E"
> mode(M) <- "logical"


Whoops. That apply() line should either come _after_ conversion to logical, or read

apply(M, 1, function(i) sum(2^(0:4) * i))

As in:

> M <- as.matrix(do.call(expand.grid, rep(list(0:1),5)))
> apply(M, 1, function(i) sum(2^(0:4) * i))
 [1]  0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
[26] 25 26 27 28 29 30 31


--
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: [hidden email]  Priv: [hidden email]

______________________________________________
[hidden email] 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.