generate combinations of elements

喜欢而已 提交于 2019-12-11 04:59:47

问题


I have a vector of elements of 1:3

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

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

回答1:


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"


来源:https://stackoverflow.com/questions/43443196/generate-combinations-of-elements

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