Learn R Programming

tidyplus (version 0.2.0)

only: Extract the only distinct value from a vector

Description

Extracts the only distinct value from an atomic vector or throws an informative error if no values or multiple distinct values.

Usage

only(x, na_rm = FALSE)

Value

The only distinct value from a vector otherwise throws an error.

Arguments

x

An atomic vector.

na_rm

A flag indicating whether to exclude missing values.

Details

only() is useful when summarizing a vector by group while checking the assumption that it is constant within the group.

See Also

Examples

Run this code
only(c(1, 1))
only(c(NA, NA))
only(c(1, 1, NA), na_rm = TRUE)
try(only(character(0)))
try(only(c(1, NA)))
try(only(c(1, 2)))

Run the code above in your browser using DataLab