Locate index of rows in a dataframe that have the value of NA

Jin-Dominique picture Jin-Dominique · Nov 10, 2013 · Viewed 41.7k times · Source

Suppose that we have the following data frame:

>  dataset1
      x
  1   1
  2   2
  3   3
  4   NA
  5   5

I want to come up with a R command that computes the row index of the 1-column data frame that contains the value of 'NA'. More specifically, in above dataset1 example, such command would return 4 - because the 'NA' appears in the 4th row of the data frame. How can I make this happen? thank you!

Answer

Jilber Urbina picture Jilber Urbina · Nov 10, 2013

As suggested by Ben Bolker, you can use both which and is.na as in:

> which(is.na(dataset1), arr.ind=TRUE)
  row col
4   4   1  # NA is in row 4 and column 1