unique
Unique Values

Description

Returns the values of the input without repetition. This function is generic.

Usage

unique(x, incomparables = FALSE, ...)
## Default S3 method:
unique(x, incomparables = FALSE, fromLast = FALSE, nmax = NA, ...)
## S3 method for class 'data.frame':
unique(x, incomparables = FALSE, fromLast = FALSE, ...)
## S3 method for class 'matrix':
unique(x, incomparables = FALSE , MARGIN = 1, fromLast = FALSE, ...)
## S3 method for class 'array':
unique(x, incomparables = FALSE , MARGIN = 1, fromLast = FALSE, ...)
## S3 method for class 'POSIXlt':
unique(x, incomparables = FALSE, ...)

Arguments

x a vector. Missing values (NAs) and Infs are allowed.

Some methods handle other objects, such as data frames, matrices, or arrays.

incomparables a vector of values that cannot be compared. Each value in x that matches a value in this vector is considered unique. If incomparables=FALSE (the default) or incomparables=NULL, all values can be compared.
fromLast a logical flag. If TRUE, compares the duplicated values from the last to the head. If FALSE (the default), compares the duplicated values from the head to the last.
nmax a positive integer or NA. If a positive integer is given then it is considered an error if x contains more than nmax unique values.
MARGIN a single integer; the array margin to be held.
... arguments for particular methods. Many of the methods will call the default method and those will pass a possible nmax argument to it.

Details

Use the incomparables argument to select values that always should be considered unique. (The values TRUE and FALSE are always able to be duplicated.)
When x is a data frame, a matrix, or an array, incomparables should always be FALSE.
If x is a data frame, then unique(x) returns the unique rows.
Value
returns an object like x, but with no duplicate values. The values are in the same order as x, except that repeated values are deleted.
Warning
When x is numeric, apparently equal values cannot be considered equal, because of numerical imprecision.
See Also
duplicated, sort, rep, POSIXt.
Examples
Names <- c("Mike", "Al", "Larry", "Al", "Al", "Zeus", "Mike", "Larry")
sort(unique(Names))  # sorted list of Names with no duplicates

x <- c(-1/0, -1/0, -2, -2, 2, 2, 1/0, 3/0) unique(x, incomp=c(1/0, -1/0)) # treat all infinities as unique

## POSIXlt x <- as.POSIXlt("1970-01-01") unique(c(x, x+1, x, x+1, NA))

hasLotsOfUniqueValues <- function(x, nmax) { # for very long x, unique(x) can use a lot of memory. # This function avoids that. tryCatch({unique(x, nmax=nmax); FALSE}, error=function(e)TRUE) } hasLotsOfUniqueValues(seq_len(10000000), nmax=100) hasLotsOfUniqueValues(seq_len(75), nmax=100)

Package base version 6.0.0-69
Package Index