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

Example: Finding Index Position of First Non-NA Value in Vector

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:

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top