This is substantially faster than which.max(is.na(x))
firstNA(x)a reversed vector
x <- c(FALSE, NA, TRUE)
firstNA(x)
#> [1] 2
reverse_vector(x)
#> [1] TRUE NA FALSE
if (FALSE) { # \dontrun{
x <- 1:1e7
system.time(rev(x))
system.time(reverse_vector(x))
} # }