This question already has an answer here:
这个问题在这里已有答案:
- Generate all possible permutations (or n-tuples) 1 answer
- 生成所有可能的排列(或n元组)1个答案
Is there a straightforward way to generate all possible permutations of a vector of integers (1 to max 999) that specifically excludes duplicated elements?
是否有一种直接的方法来生成一个整数向量(1到最大999)的所有可能的排列,这些排列明确地排除了重复的元素?
For example, for a vector with three elements in a range of 1 to 9 the sequence 1 2 3
would be acceptable, as would 1 2 9
but 1 2 2
would be invalid. The sequence must contain exactly n
elements (in this case, three). EDIT: to avoid confusion, the order is significant, so 1 2 9
and 9 2 1
are both valid and required.
例如,对于具有1到9范围内的三个元素的向量,序列1 2 3是可接受的,1 2 9但是1 2 2将是无效的。序列必须包含n个元素(在本例中为3个)。编辑:为避免混淆,订单很重要,因此1 2 9和9 2 1都是有效且必需的。
There are many questions on permutations and combinations using R on SO (such as this and this) but none that seem to fit this particular case. I'm hoping there's an obscure base R or package function out there that will take care of it without me having to write a graceless function myself.
关于在SO上使用R的排列和组合有很多问题(例如这个和这个),但似乎没有一个适合这种特殊情况。我希望有一个模糊的基础R或包功能,它将照顾它,而我不必自己编写一个没有优雅的功能。
3 个解决方案
#1
19
Using gtools
package:
使用gtools包:
require(gtools)
permutations(n = 9, r = 3, v = 1:9)
# n -> size of source vector
# r -> size of target vector
# v -> source vector, defaults to 1:n
# repeats.allowed = FALSE (default)
#2
9
EDIT: This is not what the OP asked for, but I leave this answer, to avoid confusion.
编辑:这不是OP要求的,但我留下这个答案,以避免混淆。
My math is a little bit rusty, but i think you are describing combinations, not permutations. The base functioncombn()
returns combinations.
我的数学有点生疏,但我认为你在描述组合,而不是排列。基本functioncombn()返回组合。
I illustrate with a manageable set - all combinations of length 3, from the vector 1:4
:
我用一个可管理的集合来说明 - 长度为3的所有组合,从向量1:4:
combn(4, 3)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
The difference between combinations
and permutations
is that in combinations
the order doesn't matter. So, (2, 3, 4)
and (4, 3, 2)
is the same combination, but different permutations.
组合和排列之间的区别在于组合顺序无关紧要。因此,(2,3,4)和(4,3,2)是相同的组合,但是不同的排列。
#3
8
utils::combn
; combinat::combn
or combinat::permn
are alternatives.
utils :: combn; combinat :: combn或combinat :: permn是替代品。
#1
19
Using gtools
package:
使用gtools包:
require(gtools)
permutations(n = 9, r = 3, v = 1:9)
# n -> size of source vector
# r -> size of target vector
# v -> source vector, defaults to 1:n
# repeats.allowed = FALSE (default)
#2
9
EDIT: This is not what the OP asked for, but I leave this answer, to avoid confusion.
编辑:这不是OP要求的,但我留下这个答案,以避免混淆。
My math is a little bit rusty, but i think you are describing combinations, not permutations. The base functioncombn()
returns combinations.
我的数学有点生疏,但我认为你在描述组合,而不是排列。基本functioncombn()返回组合。
I illustrate with a manageable set - all combinations of length 3, from the vector 1:4
:
我用一个可管理的集合来说明 - 长度为3的所有组合,从向量1:4:
combn(4, 3)
[,1] [,2] [,3] [,4]
[1,] 1 1 1 2
[2,] 2 2 3 3
[3,] 3 4 4 4
The difference between combinations
and permutations
is that in combinations
the order doesn't matter. So, (2, 3, 4)
and (4, 3, 2)
is the same combination, but different permutations.
组合和排列之间的区别在于组合顺序无关紧要。因此,(2,3,4)和(4,3,2)是相同的组合,但是不同的排列。
#3
8
utils::combn
; combinat::combn
or combinat::permn
are alternatives.
utils :: combn; combinat :: combn或combinat :: permn是替代品。