R: Count Number of NA Values in Each Column


You can use the following methods to count the number of NA values in each column of a data frame in R:

Method 1: Count NA Values in Each Column Using Base R

sapply(df, function(x) sum(is.na(x)))

Method 2: Count NA Values in Each Column Using dplyr

library(dplyr)

df %>% summarise(across(everything(), ~ sum(is.na(.))))

The following examples show how to use each method with the following data frame in R:

#create data frame
df <- data.frame(team=c('A', 'B', 'C', 'D', 'E'),
                 points=c(99, 90, 86, 88, NA),
                 assists=c(33, NA, NA, 39, 34),
                 rebounds=c(30, 28, 24, 24, 28))

#view data frame
df

  team points assists rebounds
1    A     99      33       30
2    B     90      NA       28
3    C     86      NA       24
4    D     88      39       24
5    E     NA      34       28

Example 1: Count NA Values in Each Column Using Base R

The following code shows how to count the number of NA values in each column using the sapply() function from base R:

#count NA values in each column
sapply(df, function(x) sum(is.na(x)))

    team   points  assists rebounds 
       0        1        2        0 

From the output we can see:

  • The team column has 0 NA values.
  • The points column has 1 NA value.
  • The assists column has 2 NA values.
  • The rebounds column has 0 NA values.

Note: The sapply() function can be used to apply a function to each column in the data frame. In this example, we apply a function that counts the total number of elements equal to NA.

Example 2: Count NA Values in Each Column Using dplyr

The following code shows how to count the number of NA values in each column using the summarise() function from the dplyr package:

#count NA values in each column
sapply(df, function(x) sum(is.na(x)))

    team   points  assists rebounds 
       0        1        2        0 

From the output we can see:

  • The team column has 0 NA values.
  • The points column has 1 NA value.
  • The assists column has 2 NA values.
  • The rebounds column has 0 NA values.

These results match the ones from the previous example.

Note: The dplyr method tends to be faster than the base R method when working with extremely large data frames.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use na.omit in R
How to Use complete.cases in R
How to Remove Empty Rows from Data Frame in R

2 Replies to “R: Count Number of NA Values in Each Column”

  1. I believe there is an error in this entry, because the examples for both base R and dplyr are exactly the same. Although it is easy to understand that the dplyr method uses summarise(…), as mentioned in the first part, I believe it would be useful to have an example for both methods. Thank you.

    1. Hi Vanessa…You’re absolutely right — thanks for pointing that out! In many tutorials or blog entries, especially when showing both base R and **dplyr** methods, it’s important to distinguish them clearly to avoid confusion. Here’s how both methods should be correctly demonstrated when **counting the number of `NA` values in each column**:

      —

      ### ✅ **Base R method:**

      “`r
      # Count NAs in each column using base R
      colSums(is.na(df))
      “`

      This works because `is.na(df)` returns a logical matrix (TRUE for NA), and `colSums` counts how many TRUEs (i.e., NAs) per column.

      —

      ### ✅ **dplyr method:**

      “`r
      library(dplyr)

      # Count NAs in each column using dplyr
      df %>%
      summarise(across(everything(), ~sum(is.na(.))))
      “`

      – `across(everything(), ~sum(is.na(.)))` applies the function to each column.
      – `summarise(…)` returns a single-row summary with counts of NA values.

      —

      If you’re suggesting this for an article or documentation page (like a tutorial or blog post), your feedback is 100% on point — providing both examples explicitly ensures clarity for all readers, especially beginners.

Leave a Reply

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