I think this is what you need. @bill was at the ball with a recommendation to combine uniqueand combn. We will also use the apply family to generate ALL combinations. Since it uniqueremoves duplicate rows, we need to wrap the results from combnto uniquethem. Then we transfer them back before returning to the screen so that each column represents a unique answer.
x <- c(rep("A", 4), rep("B",5))
zz <- sapply(seq_along(x), function(y) combn(x,y))
#Filter out all the duplicates
sapply(zz, function(z) t(unique(t(z))))
What 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 permutations, not combinations, the answer above is not so useful. This post describes a function to create unique permutations based on a set of parameters. I have no idea if it can be improved, but here one approach uses this 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)
zz <- ifelse(zz <= 4, "A", "B")
unique(zz)