only {tidyplus} | R Documentation |
Extracts the only distinct value from an atomic vector or throws an informative error if no values or multiple distinct values.
only(x, na_rm = FALSE)
x |
An atomic vector. |
na_rm |
A flag indicating whether to exclude missing values. |
only()
is useful when summarizing a vector by group
while checking the assumption that it is constant within the group.
The only distinct value from a vector otherwise throws an error.
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)))