1

I need to make a matrix/data frame containing all combinations of the elements in two vectors. All combinations must be unique, and include different elements. I know I can use the following to make a list of all combinations:

a<-c("cat","dog","cow")
b<-c("dog","cow","sheep")
combination<-as.matrix(expand.grid(a,b))

And that I can remove entries where both elements are the same using this:

combination1<-combination[combination[,1]!=combination[,2],]

Which gives the following output:

> combination1
     Var1  Var2   
[1,] "cat" "dog"  
[2,] "cow" "dog"  
[3,] "cat" "cow"  
[4,] "dog" "cow"  
[5,] "cat" "sheep"
[6,] "dog" "sheep"
[7,] "cow" "sheep"

What I need is to detect/remove rows with the same strings, but in a different order (rows 2 and 4 are "cow,dog", and "dog,cow". Is there a simple way to do this in R? I'm writing a script to test interactions between genes in barley which is very lengthy, and I want to avoid testing the same combination twice. Any help would be appreciated.

Ryan
  • 121
  • 1
  • 1
  • 11
  • Hi Ryan, follow the link to the question, but you are looking for `comb`... Try `combn( unique(c(a,b)) , 2 )` – Simon O'Hanlon Jun 25 '13 at 21:14
  • Thank you #Simon0101. "combn" doesn't work for me, because I only want to make combinations of elements that are present in both vectors (if one vector had "cow" and "goat", but not the other vector, I would not compare these). However, there was one answer on the link you gave me where they made a new function (expand.grid.unique) that did what I wanted. – Ryan Jun 25 '13 at 21:30

1 Answers1

1

You could try sorting the rows, then taking the unique ones:

>combination1 <- unique(t(apply(combination, 1, sort)))
>combination1
     [,1]  [,2]   
[1,] "cat" "dog" 
[2,] "dog" "dog"  
[3,] "cow" "dog"  
[4,] "cat" "cow"  
[5,] "cow" "cow"  
[6,] "cat" "sheep"
[7,] "dog" "sheep"
[8,] "cow" "sheep"
Scott Ritchie
  • 10,293
  • 3
  • 28
  • 64