22

Is there a straightforward way to generate all possible permutations of a vector of integers (1 to max 999) that specifically excludes duplicated elements?

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.

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.

Community
  • 1
  • 1
SlowLearner
  • 7,907
  • 11
  • 49
  • 80
  • Yes, the order is important as well as the elements themselves. – SlowLearner Feb 05 '13 at 09:37
  • 2
    Well, the answer without the edited clarification was surely `sample` and I'm sure this is a duplicate, but the cited duplicate is not a good answer. – IRTFM Oct 29 '13 at 18:51
  • As a warning to those who follow: the number of permutations of `n` items is `n!`, which gets big really fast. For the 999 elements mentioned in this question there are about 4 * 10^2564 permutations. – Gregor Thomas Apr 09 '15 at 19:17
  • 5
    This question is not a duplicate, as the linked question does not actually ask about permutations, despite having this word in its title. – liori May 05 '16 at 11:00
  • This question also does not seem to be just about permutations. `1 2 9` is not a permutation of `1 2 3`. The question is unclear to me, though. What role does 999 have? Is this the question? For some set S (say, integers from 1 to 9) and some positive integer n, how can I get all unique vectors of n elements such that each element is in S? – randy Jul 09 '23 at 23:19

3 Answers3

32

Using gtools package:

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)
blakeoft
  • 2,370
  • 1
  • 14
  • 15
Arun
  • 116,683
  • 26
  • 284
  • 387
12

utils::combn ; combinat::combn or combinat::permn are alternatives.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
11

EDIT: This is not what the OP asked for, but I leave this answer, to avoid confusion.

My math is a little bit rusty, but i think you are describing combinations, not permutations. The base functioncombn() returns combinations.

I illustrate with a manageable set - all combinations of length 3, from the vector 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.

Andrie
  • 176,377
  • 47
  • 447
  • 496
  • This is not all possible permutations, is it? ex: (4,3,2) isn't here...? – Arun Feb 05 '13 at 09:35
  • @Andrie I'm certain my math is rustier. But doesn't the above require a check after generation to remove duplicates? Or should I be reading down the columns rather than across the rows of the matrix above? – SlowLearner Feb 05 '13 at 09:36
  • 2
    @SlowLearner, you should read down columns. So, you were looking for combinations..? – Arun Feb 05 '13 at 09:37
  • @Arun But the order is also significant. So `2 3 4` and `4 3 2` are different but both are required. – SlowLearner Feb 05 '13 at 09:39
  • 1
    @SlowLearner, that is what my answer gives, but this is just combinations. – Arun Feb 05 '13 at 09:40