生成元素组合 [英] generate combinations of elements

查看:50
本文介绍了生成元素组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个元素为 1:3

我想生成这些元素的可能组合元素,这样我只有 1-2 1-3 2-3

I want to generate the possible combinations of these elements so that I only have 1-2, 1-3 and 2-3.

我尝试使用 expand.grid ,但在此情况下得到所有可能的结果不是我想要的如何轻松获得前三个呢?

I've tried with expand.grid but get ALL possible ones when this is not what I want. How do I get the main three easily?

expand.grid(1:3,1:3)
  Var1 Var2
1    1    1
2    2    1
3    3    1
4    1    2
5    2    2
6    3    2
7    1    3
8    2    3
9    3    3


推荐答案

combn(x = 1:3, m = 2, FUN = paste, collapse = "-")
#[1] "1-2" "1-3" "2-3"

#OR

apply(X = combn(1:3,2), MARGIN = 2, FUN = paste, collapse = "-")
#[1] "1-2" "1-3" "2-3"

这篇关于生成元素组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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