Next: unix Up: MacAnova Help File Previous: typeof()   Contents

unique()

Usage:
unique(x [,index:T, fuzz:d, relative:T]), x REAL, LOGICAL or CHARACTER,
  d >= 0 a REAL scalar



Keywords: ordering, variables
                                  Usage
unique(x) computes a vector consisting of all the distinct non-MISSING
values in the REAL, LOGICAL or CHARACTER vector, matrix, or array x.
When x is REAL or LOGICAL, it is an error when all its elements are
MISSING.  If x1 contains the same values of x in a different order,
unique(x1) will return the same values as unique(x), but possibly in a
different order.

                             Keyword 'index'
unique(x, index:T) computes a vector J of positive integers such that
x[J] is the same as unique(x).  That is it finds the subscripts of the
unique non-missing elements of x.

                            Inexact matching
unique(x, fuzz:d [, index:T]), where x is REAL and d >= 0 is a REAL
scalar does the same, except that, as x is scanned, x[j] is determined
to be different from x[i], 1 <= i < j only if abs(x[j] - x[i]) > d.  The
numbers returned may depend on the ordering of values in x.  That is,
for example, unique(x, fuzz:d) and unique(sort(x),fuzz:d) may return
different sets of numbers.

unique(x, fuzz:d, relative:T [, index:T]) does the same, except x[j] is
determined to be different from x[i] only if abs(x[j] - x[i]) > D, where
D = d*(abs(x[j]) + abs(x[i])).

                                Examples
Examples:
  Cmd> unique(vector(5,3,1,2,4,2,5,7,2,7))
  (1)           5           3           1           2           4
  (6)           7

  Cmd> unique(vector(5.1,3,2.9,3.5,5,2.6), fuzz:.15)
  (1)         5.1           3         3.5         2.6

  Cmd> unique(sort(vector(5.1,3,2.9,3.5,5,2.6)), fuzz:.15)#order differs
  (1)         2.6         2.9         3.5           5

  Cmd> x <- vector(run(3), run(3)+1e-4)

  Cmd> unique(x,fuzz:4e-5)
  (1)           1           2           3      1.0001      2.0001
  (6)      3.0001

  Cmd> unique(x,fuzz:4e-5,relative:T)
  (1)           1           2           3      1.0001

  Cmd> unique(vector(T,T,T,F,T))
  (1) T       F

  Cmd> paste(unique(vector("B","C","A","B","D","A","A")))
  (1) "B C A D"

  Cmd> unique(vector("B","C","A","B","D","A","A"), index:T)
  (1)            1            2            3            5

If x is a REAL or CHARACTER vector,

  Cmd> a <- factor(match(x,unique(x)))

computes a factor each level of which corresponds to a unique value of
vector x and

  Cmd> a <- factor(match(x,sort(unique(x))))

does the same except the factor levels are in the same numerical or
alphabetic order as the elements of x.

                            Cross references
See also factor(), match()..


Gary Oehlert 2006-01-30