uniq: Extract Unique Elements and Count Number of Unique Elements

View source: R/uniq.R

uniqR Documentation

Extract Unique Elements and Count Number of Unique Elements

Description

The function uniq returns a vector or data frame with duplicated elements removed. By default, missing values are omitted and unique elements are sorted increasing. The function uniq.n counts the number of unique elements in a vector or for each column in a matrix or data frame. By default, missing values are omitted before counting the number of unique elements.

Usage

uniq(data, ..., na.rm = TRUE, sort = TRUE, decreasing = FALSE, digits = NULL,
     table = TRUE, check = TRUE )

uniq.n(data, ..., na.rm = TRUE, digits = NULL, check = TRUE)

Arguments

data

a vector, factor, matrix, or data frame.

...

an expression indicating the variable names in data, e.g., uniq(dat, x1, x2) for selecting the variables x1 and x2 from the data frame dat. Note that the operators ., +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

na.rm

logical: if TRUE (default), missing values are omitted before extracting unique elements.

sort

logical: if TRUE (default), unique elements are sorted after.

decreasing

logical: if TRUE, unique elements are sorted decreasing.

digits

an integer value indicating the number of decimal places to be used when rounding numeric values before extracting unique elements. By default, unique elements are extracted without rounding, i.e., digits = NULL.

table

logical: if TRUE (default), unique elements are printed in a data frame, if FALSE unique elements are printed in a list.

check

logical: if TRUE (default), argument specification is checked.

Details

The function uniq is a wrapper function in the form of sort(unique(na.omit(x))), while the function uniq.n is a wrapper function in the form of length(unique(na.omit(x))).

Value

Returns a vector, factor, data frame, or list.

Author(s)

Takuya Yanagida

References

Becker, R. A., Chambers, J. M., & Wilks, A. R. (1988). The New S Language. Wadsworth & Brooks/Cole.

See Also

df.duplicated, df.unique

Examples

#----------------------------------------------------------------------------
# Extract Unique Elements, uniq() function

# Example 1a: Extract unique elements in a vector
uniq(airquality, Ozone)

# Example 1b: Extract unique elements in a vector, round elements
uniq(airquality, Wind, digits = 0)

# Example 1b: Extract unique elements in a vector, do not sort
uniq(airquality, Ozone, sort = FALSE)

# Example 1b: Extract unique elements in a vector, keep NA
uniq(airquality, Ozone, na.rm = FALSE)

# Example 2a: Extract unique elements in a data frame
uniq(airquality)

# Example 2a: Extract unique elements in list
uniq(airquality, table = FALSE)

#----------------------------------------------------------------------------
# Count Number of Unique Elements, uniq.n() function

# Example 3a: Count number of unique elements in a vector
uniq.n(airquality, Ozone)

# Example 1b: Count number of unique elements for each variable in a data frame
uniq.n(airquality)

misty documentation built on June 8, 2025, 1:35 p.m.

Related to uniq in misty...