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, \ldots, x_k\) with associate counts (or multiplicities) of \(n_1, n_2, \ldots, n_k\), then this function returns $$\frac{n!}{n_1!n_2!\ldots n_k!}$$ where \(n = \sum_{i=1}^{k}n_i\).
multinom(x, counts = FALSE, useDouble = FALSE, exact = FALSE)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.
if counts is TRUE, then this means x is the set of
counts \(n_1, n_2, \ldots, n_k\) rather than the set itself
kept for backwards compatibility, and no longer has any
effect. The coefficient is always computed exactly, and returned as a
double unless exact is TRUE.
if TRUE the exact result is returned as a big integer
of class bigz from the gmp package.
The multinomial coefficient for the given multiset, or given set of
multiplicities. By default this is a double: the exact value rounded to
double precision, so it is exact up to \(2^{53}\) and correct to
about 15 significant digits beyond that. If exact is TRUE
it is an exact bigz.
## 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)
#> [1] 60
## 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)
#> [1] 60
## An example of a large coefficient. x is a vector of counts
## c(12,11,8,8,6,5). The true answer from Maple is
## 11,324,718,121,789,252,764,532,876,767,840,000
## exact = TRUE gives this. The default double precision result only
## keeps the first 15 or so significant digits.
## Thanks to Lev Dashevskiy for calling my attention to this.
x = c(12,11,8,8,6,5)
multinom(x, counts = TRUE)
#> [1] 1.132472e+34
multinom(x, counts = TRUE, exact = TRUE)
#> Big Integer ('bigz') :
#> [1] 11324718121789252764532876767840000