将向量的所有组合粘贴到 R 中 [英] Paste all combinations of a vector in R

查看:54
本文介绍了将向量的所有组合粘贴到 R 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个向量说:

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

我只需要组合单对.

山姆

推荐答案

Try combn

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

这篇关于将向量的所有组合粘贴到 R 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆