Learn R Programming

zmisc (version 0.3.0)

zingle: Return the single (unique) value found in a vector

Description

zingle() returns the only value present in a vector. If the vector contains more than one distinct value, it throws an error. This is a guard for aggregations where all values within a group should be identical, but where you want that assumption checked rather than assumed. Only values are compared. Names are ignored and the result is unnamed, in line with other aggregation functions.

Usage

zingle(
  x,
  ...,
  empty.ok = FALSE,
  na.ok.partial = FALSE,
  na.ok.all = FALSE,
  nan.ok.all = FALSE
)

Value

The single element in the vector, unnamed. For na.ok.partial the non-missing value is returned.

Arguments

x

Vector of elements that should all be identical

...

Unused, reserved to force later arguments to be named

empty.ok

Is an empty vector ok?

na.ok.partial

Is a mix of NA and one distinct non-missing value ok?

na.ok.all

Is a vector of only NA values ok?

nan.ok.all

Is NaN ok as the returned value?

Examples

Run this code
zingle(c("Alpha", "Alpha", "Alpha"))
zingle(c("Alpha", NA, "Alpha"), na.ok.partial = TRUE)
zingle(c(NA, NA), na.ok.all = TRUE)
zingle(c(NaN, NaN), nan.ok.all = TRUE)

Run the code above in your browser using DataLab