问题
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