Reading ?expand.grid, there is a "see also" reference to "combn" which looks 
close to what you want.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnew...@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.

Q <quaga...@gmail.com> wrote:

Hello,

I'm trying to create a data frame where each row has a unique combination of
factors.

I start with a vector of species like so:



> 1> test <- c("A","B","C","D")
> 

> 1> test
> 

> [1] "A" "B" "C" "D"
> 

To get all species combinations I have used expand.grid like this:



> 1> pairs <- expand.grid(test,test)

> 1> pairs

> Var1 Var2

> 1 A A

> 2 B A

> 3 C A

> 4 D A

> 5 A B

> 6 B B

> 7 C B

> 8 D B

> 9 A C

> 10 B C

> 11 C C

> 12 D C

> 13 A D

> 14 B D

> 15 C D

> 16 D D
> 

Now I want to select only the unique pairs, which I have tried to do with
the function "unique":



> 1> unique(pairs)
> 

, but that doesn't do anything... I guess because it considers A,B to be
different from B,A. The data frame I would like to end up with should look
like this.



> Var1 Var2

> 1 A A

> 2 B A

> 3 C A

> 4 D A

> 6 B B

> 7 C B

> 8 D B

> 11 C C

> 12 D C

> 16 D D

> 

Thanks for your help!

Q

--
View this message in context: 
http://r.789695.n4.nabble.com/Create-a-data-frame-of-all-possible-unique-combinations-of-factors-tp3647338p3647338.html
Sent from the R help mailing list archive at Nabble.com.
        [[alternative HTML version deleted]]

_____________________________________________

R-help@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.


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to