Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paste all combinations of a vector in R

Tags:

r

paste

I have a vector say:

vec = c("A", "B", "C")

And I want to paste single combinations of every item in the vector to get the result

AB
AC
BC

I know I can use outer to get all possible combinations of the vector, but I am stumped as how to only get the result above. Order doesn't matter in this case, so the result could plausibly also be

BA
CA
CB

I just need to combine the single pairs.

Sam

like image 436
SamPassmore Avatar asked Apr 09 '15 21:04

SamPassmore


1 Answers

Try combn

 combn(vec,2, FUN=paste, collapse='')
 #[1] "AB" "AC" "BC"
like image 141
akrun Avatar answered Oct 08 '22 11:10

akrun