1

I have a list of 388 sample codes and I want all unique pairwise combinations. I've tried several options in r but it still doesn't give me what I want.

This is a part of my sample codes:

11_07LENABL017

30_08ASBBAL033

21_08GEHOAL038

21_08GEHOAL041

04_08HLNABL043

31_08MMBBAL029

39_08VOVOAL030

39_08VOVOAL031

39_08VOVOAL039


So what is want is like this:

11_07LENABL017_30_08ASBBAL033

11_07LENABL017_21_08GEHOAL038

11_07LENABL017_21_08GEHOAL041

and so on

Any ideas?

0

1 Answer 1

4

You could try the following:

x = c(1,2,3,4,5)    
apply(combn(x,2),2,paste,collapse='_')

Output:

[1] "1_2" "1_3" "1_4" "1_5" "2_3" "2_4" "2_5" "3_4" "3_5" "4_5"

or, if you consider a-b and b-a to be different:

x = c(1,2,3,4,5)
with(subset(expand.grid(x,x),Var1!=Var2),paste0(Var1,'-',Var2))

Output:

[1] "2-1" "3-1" "4-1" "5-1" "1-2" "3-2" "4-2" "5-2" "1-3" "2-3" "4-3" "5-3" "1-4" "2-4" "3-4" "5-4"
[17] "1-5" "2-5" "3-5" "4-5"

Hope this helps!

3
  • 3
    You can add the function in combn, i.e. combn(x, 2, paste, collapse = '_')
    – Sotos
    Feb 24, 2018 at 9:42
  • Nice, I did not know that!
    – Florian
    Feb 24, 2018 at 9:44
  • Thanx Floriean, that works perfectly! Feb 24, 2018 at 10:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.