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, list or data frame with duplicated elements removed. By default, the function prints a data frame with missing values omitted and unique elements 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, write = NULL, append = TRUE, check = TRUE, output = 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. Note that missing values are always omitted when writing the output into an Excel file, i.e., na.rm = TRUE.

sort

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

decreasing

logical: if TRUE, unique elements are sorted decreasing when specifying sort = TRUE

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. Note that unique elements are always printed in a data frame when writing the output into an Excel file, i.e. table = TRUE.

write

a character string naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

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

output

logical: if TRUE (default), output is shown on the console.

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 an object of class misty.object when using the uniq function, which is a list with following entries:

call

function call

type

type of analysis

data

a vector, factor, matrix, or data frame

args

specification of function arguments

result

list with unique elements

or a vector with the count number of unique elements for a vector, factor or each column in a matrix or data frame when using the uniq.n function.

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 Aug. 18, 2025, 5:16 p.m.

Related to uniq in misty...