Learn R Programming

sfsmisc (version 0.9-4)

uniqueL: A Reversable Version of unique()

Description

A version of unique keeping enough information to reverse (or invert) to the original data.

Usage

uniqueL(x, isuniq = !duplicated(x))

Arguments

x
numeric vector, of length n, say.
isuniq
logical vector of the same length as x. For the reversion to work this should select at least all unique values of x.

Value

  • list of two components,
  • ixinteger vector of indices
  • xUvector of values from x
  • such that both x[isuniq] === xU and xU[ix] === x.

See Also

unique, duplicated.

Examples

Run this code
x0 <- c(1:3,2:7,8:4)
 str(r0 <- uniqueL(x0))
 with(r0, xU[ix]) ## == x0 !
<testonly>for(n in 1:100) {
   x0 <- round((1+rpois(1,lam=1))*rnorm(40))
   r0 <- uniqueL(x0)
   stopifnot(sort(unique(x0)) == r0$xU,
	     with(r0, xU[ix]) == x0)
 }</testonly>

Run the code above in your browser using DataLab