\name{permutation}
\alias{permutation}
\alias{npermutation}
\title{ Permutations }
\description{
  Calculating and creating permutations
}
\usage{
npermutation(k, n, replace = FALSE)
permutation(k, n=NULL, x=NULL, replace = FALSE)
}
\arguments{
  \item{k}{ number of positions }
  \item{n}{ poolsize }
  \item{x}{ vector pool of values, can be given instead of n }
  \item{replace}{ default FALSE, TRUE to allow reusing of pool elements }
}
\details{
  \code{npermutation} returns the number of permutations, \code{permutation} returns a matrix of permutations. No special sequence granted. Implemented in C, but not faster than the functions in gregmisc.
}
\value{
  A matrix of (k x npermutation)
}
\author{ Jens Oehlschlägel }
\seealso{ \code{\link{combination}}, \code{\link{margination}}, \code{\link[base]{expand.grid}}, \code{\link[gregmisc]{combinations}} }
\examples{
  npermutation(2,4)
  permutation(2,4)
  permutation(2,x=letters[1:4])
  npermutation(2,4,replace=TRUE)
  permutation(2,4,replace=TRUE)

  \dontrun{
  ## regression test (error in gregmisc for 1.5.1)
  require(gregmisc)
  k <- 3
  n <- 6
  replace <- FALSE
  a <- sort(apply(permutation(k,n,replace=replace), 2, paste, collapse="-"))
  b <- sort(apply(permutations(n,k,rep=replace), 1, paste, collapse="-"))
  stopifnot(length(a)==npermutation(k,n,replace=replace))
  stopifnot(identical(a,b))
  replace <- TRUE
  a <- sort(apply(permutation(k,n,replace=replace), 2, paste, collapse="-"))
  b <- sort(apply(permutations(n,k,rep=replace), 1, paste, collapse="-"))
  stopifnot(length(a)==npermutation(k,n,replace=replace))
  stopifnot(identical(a,b))
  }
}
\keyword{ iteration }
