# Count NA Values in R (3 Examples)

In this R tutorial you’ll learn how to determine the number of NA values in a vector or data frame column.

The page is structured as follows:

Let’s start right away…

## Example 1: Count NA Values in Vector

Example 1 shows how to determine the amount of NA values in a vector.

First, we have to create an example vector with NA values:

```vec <- c(3, 1, NA, 3, NA, NA, 4)
vec
# 3  1 NA  3 NA NA  4```

As you can see, our example vector contains several numeric values and NAs. If we want to count the number of NA values in our example vector, we can use a combination of the sum and is.na functions:

```sum(is.na(vec))
# 3```

After running the previous code, the RStudio console returns the value 3, i.e. our example vector contains 3 NA values.

## Example 2: Count NA Values in Data Frame Column

We can apply a similar R syntax as in Example 1 to determine the number of NA values in a data frame column. First, we need to create some example data:

```data <- data.frame(x1 = c(NA, 5, 5, NA, 1, 2),
x2 = c(1, 2, 3, NA, 5, 6),
x3 = 1)
data
#   x1 x2 x3
# 1 NA  1  1
# 2  5  2  1
# 3  5  3  1
# 4 NA NA  1
# 5  1  5  1
# 6  2  6  1```

Our example data consists of six rows and three columns. The variables x1 and x2 contain NA values.

Let’s assume that we want to extract the amount of NA values on the variable x1. Then, we can use the following R code:

```sum(is.na(data\$x1))
# 2```

The variable x1 contains 2 NAs.

## Example 3: Count NA Values in All Data Frame Columns

We can also count the NA values of multiple data frame columns by using the colSums function instead of the sum function. Have a look at the following R code:

```colSums(is.na(data))
# x1 x2 x3
#  2  1  0```

The RStudio console output shows the number of NA values for each of our variables. The variable x1 contains 2 NA values, the variable x2 contains 1 NA value, and the variable x3 contains no NA values.

## Video & Further Resources

Have a look at the following video of my YouTube channel. I’m explaining the R syntax of this tutorial in the video.

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party. If you accept this notice, your choice will be saved and the page will refresh.

Furthermore, you could have a look at some of the other articles which I have published on my website. Some articles can be found below:

To summarize: At this point you should know how to different ways how to count NA values in vectors, data frame columns, and variables in the R programming language. Please let me know in the comments section, in case you have any additional questions and/or comments.

Subscribe to the Statistics Globe Newsletter

• Rubina Bhati
June 25, 2021 12:06 pm

dear sir, your video is really helpful, could you please tell me how to take percentage column wise for the NA values

• June 25, 2021 1:27 pm

Hey Rubina,

Thank you, glad you like the tutorial!

You can calculate NA percentages by column using the following R code:

`(colSums(is.na(data)) / nrow(data)) * 100`

Regards

Joachim

• • December 22, 2021 6:55 am