Return Index Position of First Non-NA Value in R (Example Code)
In this R tutorial you’ll learn how to find the first vector element that is not NA.
Creating Example Data
x <- c(NA, NA, NA, NA, NA, 99, NA, NA, 99, NA) # Example vector in R x # Display example vector in RStudio console # [1] NA NA NA NA NA 99 NA NA 99 NA |
x <- c(NA, NA, NA, NA, NA, 99, NA, NA, 99, NA) # Example vector in R x # Display example vector in RStudio console # [1] NA NA NA NA NA 99 NA NA 99 NA
Example: Finding Index Position of First Non-NA Value in Vector
which(!is.na(x))[1] # Get index of first actual value # [1] 6 |
which(!is.na(x))[1] # Get index of first actual value # [1] 6
Related Tutorials
Below, you may find some further resources on topics such as data.table, data conversion, data inspection, and indices: