[R] Create a data frame of all possible unique combinations of factors
David Winsemius
dwinsemius at comcast.net
Wed Jul 6 02:08:20 CEST 2011
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
More information about the R-help
mailing list