Nicolas Rosewick
Nicolas Rosewick

Reputation: 1998

permutation with repetition

In R, how can I produce all the permutation of a group, but in this group there are some repetitive elements.

Example :

A = {1,1,2,2,3} 

solution :

1,1,2,2,3
1,1,2,3,2
1,1,3,2,2
1,2,1,2,3
1,2,2,1,3
1,2,2,3,1
.
.

Upvotes: 1

Views: 5093

Answers (4)

Blue Magister
Blue Magister

Reputation: 13363

If you want to do it with built-in R:

permute <- function(vec,n=length(vec)) {
  permute.index <- sample.int(length(vec),n)
  return(vec[permute.index])
}

permute(A)

Upvotes: 2

EDi
EDi

Reputation: 13280

Using the permute package:

x <- c(1,1,2,2,3)
require(permute)
allPerms(x, observed = TRUE)

Upvotes: 1

BenBarnes
BenBarnes

Reputation: 19454

using the gtools package,

library(gtools)

x <- c(1,1,2,2,3)

permutations(5, 5, x, set = FALSE)

Upvotes: 5

csgillespie
csgillespie

Reputation: 60462

Just use the combinat package:

A = c(1,1,2,2,3)
library(combinat)
permn(A)

Upvotes: 3

Related Questions