# [R] unique combinations

Keith Jewell k.jewell at campden.co.uk
Wed Dec 21 15:40:25 CET 2011

```Thanks Uwe,

I was happy that my 2 lines gave what the OP asked for, albeit in a
different order.

My puzzlement arose from Jeff Newmillers comment:
> You could read the help for expand.grid very carefully for the answer to
> this question.
... which I reas as saying that ?expand.grid would lead to a solution.
I used "[", which I couldn't see suggested on ?expand.grid.
I thought I'd missed a neater solution in the help pages, and hoped someone
could point it out.

Best regards,

Keith Jewell
------------------------------
"Uwe Ligges" <ligges at statistik.tu-dortmund.de> wrote in message
news:4EF1EC69.2080707 at statistik.tu-dortmund.de...
>
>
> On 21.12.2011 14:39, Keith Jewell wrote:
>> OK, someone point it out to me; my wife tells me I can't see what's in
>> front
>> of me :-}
>>
>> I read ?expand.grid carefully, went to ?combn and ?choose but still
>> couldn't
>> see an easy way to get what the OP asked for. The neatest I can get
>> (which
>> isn't very neat!) is:
>>
>>> myVec<- c(1,2,3)
>>> eg<- expand.grid(myVec, myVec)
>>> eg[eg[,1]<= eg[,2],]
>>    Var1 Var2
>> 1    1    1
>> 4    1    2
>> 5    2    2
>> 7    1    3
>> 8    2    3
>> 9    3    3
>>
>> What am I missing?
>
> You are just using a different order - sort accoriding to Var1...
>
> Best,
> Uwe Ligges
>
>>
>> Best regards,
>>
>> KJ
>> "Jeff Newmiller"<jdnewmil at dcn.davis.ca.us>  wrote in message
>> news:80cc9ff9-d867-4dfc-b6e3-9c1df782209a at email.android.com...
>>> You could read the help for expand.grid very carefully for the answer to
>>> this question.
>>> ---------------------------------------------------------------------------
>>> Jeff Newmiller                        The     .....       .....  Go
>>> Live...
>>> DCN:<jdnewmil at dcn.davis.ca.us>         Basics: ##.#.       ##.#.  Live
>>> Go...
>>>                                       Live:   OO#.. Dead: OO#..  Playing
>>> Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
>>> /Software/Embedded Controllers)               .OO#.       .OO#.
>>> rocks...1k
>>> ---------------------------------------------------------------------------
>>> Sent from my phone. Please excuse my brevity.
>>>
>>> Antje Niederlein<niederlein-rstat at yahoo.de>  wrote:
>>>
>>>> Hi there,
>>>>
>>>> I have a vector and would like to create a data frame, which contains
>>>> all unique combination of two elements, regardless of order.
>>>>
>>>> myVec<- c(1,2,3)
>>>>
>>>> what expand.grid does:
>>>>
>>>> 1,1
>>>> 1,2
>>>> 1,3
>>>> 2,1
>>>> 2,2
>>>> 2,3
>>>> 3,1
>>>> 3,2
>>>> 3,3
>>>>
>>>> what I would like to have
>>>>
>>>> 1,1
>>>> 1,2
>>>> 1,3
>>>> 2,2
>>>> 2,3
>>>> 3,3
>>>>
>>>> Can anybody help?
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help