This function calculates the number of permutations of a multiset, this being the multinomial coefficient. If a set X contains k unique elements x_1, x_2, …, x_k with associate counts (or multiplicities) of n_1, n_2, …, n_k, then this function returns

n!/(n_1!n_2!… n_k!)

where n = n_1 + n_2 + cdots + n_k.

Usage

multinom(x, counts = FALSE)

Arguments

x

Either a multiset (with one or more potentially non-unique elements), or if counts is TRUE a set of counts of the unique elements of X. If counts is FALSE and x is not numeric, then x will be coerced into an integer vector internally. If counts is TRUE then x must be a vector of integers that are greater than, or equal to zero.

counts

if counts is TRUE, then this means x is the set of counts n_1, n_2, …, n_k rather than the set itself

## an example with a multiset X = (a,a,a,b,b,c)
## There are 3 a s, 2 b s and 1 c, so the answer should be
## (3+2+1)!/(3!2!1!) = 6!/3!2!1! = 60
x = rep(letters[1:3],3:1)
multinom(x)
## in this example x is a vector of counts
## the answer should be the same as above as x = c(3,2,1)
x = rep(letters[1:3],3:1)
x = as.vector(table(x)) #coerce x into a vector of counts
multinom(x, counts = TRUE)