Paste all combinations of a vector in R

烂漫一生 提交于 2019-11-26 22:05:50

问题


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"


来源:https://stackoverflow.com/questions/29549562/paste-all-combinations-of-a-vector-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!