How can I check whether my data frame contains NA/Inf values in some column or not in R?




How can I know whether my data frame contains NA/-Inf/Inf values or not? I want to know the variable name too in which the missing or infinite values are present.



The two functions you are looking for are and is.infinite. You can test for both by wrapping them with the function any. So any( will return TRUE if any of the values of the object are NA. And any(is.infinite(x)) will return the same for -Inf or Inf.

If you would like to check this over a data frame, apply will help.

apply(df, 2, function(x) any(

Will test the condition by column.

In the case that you would like to test for both conditions, you can add the pipe operator, |.

any( | is.infinite(x))

It reads, “Does any member of the variable x have the value NA or -Inf or Inf?”

To your second question, if you want to learn the names of the column with these values, assign the output of our apply function above to a variable, and subset the column names by that new variable:

indx <- apply(df, 2, function(x) any( | is.infinite(x)))