These functions retrieve the nth, first or last non-NA
value in a vector. If there are fewer than n
non-NA
values, a default value can be returned.
Value
nth_valid
: The nth non-NA
value inx
.first_valid
: The first non-NA
value inx
.last_valid
: The last non-NA
value inx
.
Examples
x <- c(NA, 7, NA, 5, 4, NA, 2, NA)
first_valid(x)
#> [1] 7
last_valid(x)
#> [1] 2
nth_valid(x, 2)
#> [1] 5
nth_valid(x, -2)
#> [1] 4
nth_valid(x, 6)
#> [1] NA
nth_valid(x, 6, default = -Inf)
#> [1] -Inf