Create a data frame of all possible unique combinations of factors

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

Create a data frame of all possible unique combinations of factors

Q
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Create a data frame of all possible unique combinations of factors

jholtman
Is this what you want:

>  test <- c("A","B","C","D")
> expand.grid(test,test)
   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


On Tue, Jul 5, 2011 at 7:16 PM, Q <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

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

Re: Create a data frame of all possible unique combinations of factors

David Winsemius
In reply to this post by Q

On Jul 5, 2011, at 7:16 PM, Q 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
>

snipped
> Now I want to select only the unique pairs, which I have tried to do  
> with
> the function "unique":
>
>
>
>> 1> unique(pairs)

  You want the duplicated function or more precisely its negation, ...  
and you need to sort within rows if yoy want (b,a) to look like (a,b).

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

 > pairs[!duplicated(t(apply(pairs, 1, sort))), ]
    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

The t( ) is needed to get the row-wise arrangement restore after apply  
transposed it.

> [[alternative HTML version deleted]]
Nabble allows posting in plain text. You should.

>
PLease --->
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html

--

David Winsemius, MD
West Hartford, CT

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

Re: Create a data frame of all possible unique combinations of factors

jholtman
In reply to this post by Q
Missed that you wanted to elim duplicated:

> z <- expand.grid(test,test)
> # add 'unique' key
> z$key <- apply(z, 1, function(x)paste(sort(x), collapse=''))
> str(z)
'data.frame':   16 obs. of  3 variables:
 $ Var1: Factor w/ 4 levels "A","B","C","D": 1 2 3 4 1 2 3 4 1 2 ...
 $ Var2: Factor w/ 4 levels "A","B","C","D": 1 1 1 1 2 2 2 2 3 3 ...
 $ key : chr  "AA" "AB" "AC" "AD" ...
 - attr(*, "out.attrs")=List of 2
  ..$ dim     : int  4 4
  ..$ dimnames:List of 2
  .. ..$ Var1: chr  "Var1=A" "Var1=B" "Var1=C" "Var1=D"
  .. ..$ Var2: chr  "Var2=A" "Var2=B" "Var2=C" "Var2=D"
> subset(z, !duplicated(z$key))
   Var1 Var2 key
1     A    A  AA
2     B    A  AB
3     C    A  AC
4     D    A  AD
6     B    B  BB
7     C    B  BC
8     D    B  BD
11    C    C  CC
12    D    C  CD
16    D    D  DD
>


On Tue, Jul 5, 2011 at 7:16 PM, Q <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>



--
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?

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

Re: Create a data frame of all possible unique combinations of factors

Q
This post has NOT been accepted by the mailing list yet.
In reply to this post by jholtman
CONTENTS DELETED
The author has deleted this message.
Q
Reply | Threaded
Open this post in threaded view
|

Re: Create a data frame of all possible unique combinations of factors

Q
In reply to this post by jholtman
CONTENTS DELETED
The author has deleted this message.
Reply | Threaded
Open this post in threaded view
|

Re: Create a data frame of all possible unique combinations of factors

Jeff Newmiller
In reply to this post by Q
Reading ?expand.grid, there is a "see also" reference to "combn" which looks close to what you want.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<[hidden email]> 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 <[hidden email]> 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]]

_____________________________________________

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

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