[R] unique combinations

Keith Jewell k.jewell at campden.co.uk
Wed Dec 21 14:39:31 CET 2011


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?

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
>>PLEASE do read the posting guide
>>http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list