Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Question Number of Unique Combinations of A,A,A,A,B,B,B,B,B

Tags:

r

combinations

I am trying to find a way to get a list in R of all the possible unique permutations of A,A,A,A,B,B,B,B,B.

Combinations was what was originally thought to be the method for obtaining a solution, hence the combinations answers.

like image 985
HazelnutCoffee Avatar asked May 28 '11 22:05

HazelnutCoffee


2 Answers

I think this is what you're after. @bill was on the ball with the recommendation of combining unique and combn. We'll also use the apply family to generate ALL of the combinations. Since unique removes duplicate rows, we need to transpose the results from combn before uniqueing them. We then transpose them back before returning to the screen so that each column represents a unique answer.

#Daters
x <- c(rep("A", 4), rep("B",5))
#Generates a list with ALL of the combinations
zz <- sapply(seq_along(x), function(y) combn(x,y))
#Filter out all the duplicates
sapply(zz, function(z) t(unique(t(z))))

Which returns:

[[1]]
     [,1] [,2]
[1,] "A"  "B" 

[[2]]
     [,1] [,2] [,3]
[1,] "A"  "A"  "B" 
[2,] "A"  "B"  "B" 

[[3]]
     [,1] [,2] [,3] [,4]
[1,] "A"  "A"  "A"  "B" 
[2,] "A"  "A"  "B"  "B" 
[3,] "A"  "B"  "B"  "B" 

...

EDIT Since the question is about permuations and not combinations, the answer above is not that useful. This post outlines a function to generate the unique permutations given a set of parameters. I have no idea if it could be improved upon, but here's one approach using that function:

fn_perm_list <-
 function (n, r, v = 1:n)
 {
    if (r == 1)
       matrix(v, n, 1)
    else if (n == 1)
       matrix(v, 1, r)
    else {
       X <- NULL
       for (i in 1:n) X <- rbind(X, cbind(v[i], fn_perm_list(n -
            1, r - 1, v[-i])))
        X
    }
 } 

zz <- fn_perm_list(9, 9)

#Turn into character matrix. This currently does not generalize well, but gets the job done
zz <- ifelse(zz <= 4, "A", "B")

#Returns 126 rows as indicated in comments
unique(zz)
like image 199
Chase Avatar answered Oct 11 '22 17:10

Chase


There's no need to generate permutations and then pick out the unique ones. Here's a much simpler way (and much, much faster as well): To generate all permutations of 4 A's and 5 B's, we just need to enumerate all possible ways of placing 4 A's among 9 possible locations. This is simply a combinations problem. Here's how we can do this:

x <- rep('B',9) # vector of 9 B's

a_pos <- combn(9,4) # all possible ways to place 4 A's among 9 positions

perms <- apply(a_pos, 2, function(p) replace(x,p,'A')) # all desired permutations

Each column of the 9x126 matrix perms is a unique permutation 4 A's and 5 B's:

> dim(perms)
[1]   9 126
> perms[,1:4] ## look at first few columns
      [,1] [,2] [,3] [,4]
 [1,] "A"  "A"  "A"  "A" 
 [2,] "A"  "A"  "A"  "A" 
 [3,] "A"  "A"  "A"  "A" 
 [4,] "A"  "B"  "B"  "B" 
 [5,] "B"  "A"  "B"  "B" 
 [6,] "B"  "B"  "A"  "B" 
 [7,] "B"  "B"  "B"  "A" 
 [8,] "B"  "B"  "B"  "B" 
 [9,] "B"  "B"  "B"  "B" 
like image 25
Prasad Chalasani Avatar answered Oct 11 '22 16:10

Prasad Chalasani