Home Â» How to Find and Count Missing Values in R (With Examples)

# How to Find and Count Missing Values in R (With Examples)

You can use the following methods to find and count missing values in R:

Method 1: Find Location of Missing Values

which(is.na(df\$column_name))

Method 2: Count Total Missing Values

sum(is.na(df\$column_name))

The following examples show how to use these functions in practice.

### Example 1: Find and Count Missing Values in One Column

Suppose we have the following data frame:

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

#view data frame
df

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

We can use the following code to identify which positions have missing values in the â€˜assistsâ€™ column and find the total missing values in the â€˜assistsâ€™ column:

#identify locations of missing values in 'assists' column
which(is.na(df\$assists))

[1] 1 3 4

#count total missing values in 'assists' column
sum(is.na(df\$assists))

[1] 3

From the output we can see that positions 1, 3, and 4 have missing values in the â€˜assistsâ€™ column and there are a total of 3 missing values in the column.

### Example 2: Count Missing Values in All Columns

The following code shows how to count the total missing values in every column of a data frame:

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

#count total missing values in each column of data frame
sapply(df, function(x) sum(is.na(x)))

team   points  assists rebounds
1        0        3        1

From the output we can see:

• The â€˜teamâ€™ column has 1 missing value.
• The â€˜pointsâ€™ column has 0 missing values.
• The â€˜assistsâ€™ column has 3 missing values.
• The â€˜reboundsâ€™ column has 1 missing value.

### Example 3: Count Missing Values in Entire Data Frame

The following code shows how to count the total missing values in an entire data frame:

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

#count total missing values in entire data frame
sum(is.na(df))

[1] 5

From the output we can see that there are 5 total missing values in the entire data frame.