Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating non-duplicate combination pairs in R

Tags:

r

combinations

Sorry for the non-descriptive title but I don't know whether there's a word for what I'm trying to achieve.

Let's assume that I have a list of names of different classes like

c( '1', '2', '3', '4')

I'd like to generate all possible permutation pairs out of this so that there are no reverse-duplicates. So what I'd like to have is something like

'1' '2'
'1' '3'
'1' '4'
'2' '3'
'2' '4'
'3' '4'

Note that I don't have e.g. '2' '1' because I already have '1' '2'. Is there an easy way to achieve this in R?

like image 305
lhahne Avatar asked Nov 10 '09 18:11

lhahne


1 Answers

> x<-c('1','2','3','4')
> combn(x,2)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "1"  "1"  "1"  "2"  "2"  "3" 
[2,] "2"  "3"  "4"  "3"  "4"  "4"
like image 54
Mark Avatar answered Oct 07 '22 11:10

Mark