Paste all combinations of a vector in R

2020-02-02 01:01发布

问题:

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

回答1:

Try combn

 combn(vec,2, FUN=paste, collapse='')
 #[1] "AB" "AC" "BC"


标签: r paste