[R] All possible permutations of a 3 group assignment vector

Charles C. Berry cberry at tajo.ucsd.edu
Mon Jun 21 22:29:18 CEST 2010


On Mon, 21 Jun 2010, Duplisea, Daniel wrote:

> Hello. Given this group assignment vector x:
>
> x= rep(letters[1:3],c(9,21,9))
>
> I would like to know the number of all possible unique permutations of x 
> and also obtain the matrix of them.

Do you mean what would

 	ncol( unique( replicate( Inf, sample(x) ) , MARGIN=2 ) )

be if you had a computer capable of carrying out the calculation?

Well, the answer would be choose(39,9)*choose(30,9) >  	3e+15.

So on earthly computers you have no chance of enumerating the matrix.


Thus, each vector should still 
> contain 9*a, 21*b and 9*c, but case assigment must be different for one 
> case (default at least 2) for each permuation.

??

Unless you meant what I suggested above, define 'case'. Better still, give 
an example.


a, b and c can be 
> interspersed in permuted vectors, i.e. order does not matter. If this 
> unique permutation matrix is very large then I would like to choose a 
> subset of it. I can easily permute the vector with sample(x) but if I 
> repeat this n>1 times there is no guarantee of uniqueness.

Do you really need uniqueness?

If this is for some statistical procedure, a small probability of 
duplicates will likely not matter. And you can always toss out the 
duplicates if you ever encounter them.

For a small 
> number of samples, I could look for unique vectors and keep only those 
> but this seems awkward and I suspect there are more elegant ways to do 
> it.
>
> Is there an r function which will allow me to calculate both how many 
> unique permutations of x exist and also obtain a matrix of them and/or a 
> subset of that matrix?
>

 	See ?choose for this problem, and ?unique for smaller problems.

HTH,

Chuck

> Thanks very much,
> Daniel
>
> ______________________
> Daniel Duplisea
> Pêches et Océans / Fisheries and Oceans Canada
> Institut Maurice-Lamontagne
> Mont-Joli, QC
> G5H 3Z4
> Canada
>
> tel: 1 418 775 0881
> fax: 1 418 775 0740
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

Charles C. Berry                            (858) 534-2098
                                             Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego 92093-0901



More information about the R-help mailing list