Take the first value that isn't missing. Adapted from http://stackoverflow.com/a/40515261/1082435.

first_nonmissing(x, value_if_all_na = NULL, na_codes = NULL)

Arguments

x

A vector of values to collapse.

value_if_all_na

A scalar value to return if all elements are missing (i.e., they are all either NA, or one of the na_codes).

na_codes

A vector of codes that represent missing values.

Value

A scalar of converted names.

Details

value_if_all_na and na_codesmust have the same data type as x.

If value_if_all_na is null, then an NA will be returned. If na_codes is null, then all non-NA values are considered.

Author

Will Beasley

Examples

first_nonmissing(c(NA, "b", "c"))
#> [1] "b"
first_nonmissing(c(NA_character_, NA_character_))
#> [1] NA
first_nonmissing(character(0))
#> [1] NA