A wrapper for [ that handles NA indices better when using
inside summarize
Value
If allNA_NA = FALSE and na.rm = FALSE, identical to
x[i].
If allNA_NA = FALSE and na.rm = TRUE, identical to
x[i[!is.na(i)]].
If allNA_NA = TRUE, identical to x[i] unless
all(is.na(i)) == TRUE, in which case returns NA
Details
This function was created to better handle the common situation
of extracting values by index inside summarize when some
indices may be NA.
Often this arises when using a function to find indices of interest might
fail to find any, as can sometimes happen when using
which_max_gc or which_min_gc (wrappers for which.max
and which.min, respectively),
find_local_extrema and its wrappers first_maxima and
first_minima, or find_threshold_crosses and its wrappers
first_below and first_above. When all indices are NA,
[ returns a vector of all NA of length length(x).
Since this typically raises an undesirable warning or error from
summarize, extr_val instead returns a single NA
value.
An alternative (but largely superseded) use of this function is to
extract a value by index after NA values have been removed.
This is not applicable for any common functions with gcplyr,
which all return index values for the original vector.