invPerm
Inverse Permutation Vector
Description
From a permutation vector p
, compute its inverse permutation vector.
Usage
invPerm(p, zero.p = FALSE, zero.res = FALSE)
Arguments
p | an integer vector of length, say, |
zero.p | logical indicating if |
zero.res | logical indicating if the result should contain values |
Value
an integer vector of the same length (n
) as p
. By default, (zero.p = FALSE, zero.res = FALSE
), invPerm(p)
is the same as order(p)
or sort.list(p)
and for that case, the function is equivalent to invPerm. <- function(p) { p[p] <- seq_along(p) ; p }
.
Author(s)
Martin Maechler
See Also
the class of permutation matrices, pMatrix
.
Examples
p <- sample(10) # a random permutation vector ip <- invPerm(p) p[ip] # == 1:10 ## they are indeed inverse of each other: stopifnot( identical(p[ip], 1:10), identical(ip[p], 1:10), identical(invPerm(ip), p) )
Copyright (©) 1999–2012 R Foundation for Statistical Computing.
Licensed under the GNU General Public License.