VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-find-and-count-missing-values-in-r-dataframe/

⇱ How to Find and Count Missing Values in R DataFrame - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Find and Count Missing Values in R DataFrame

Last Updated : 13 Jan, 2026

In R programming, missing values are represented using NA. Before analysis, it is important to identify where missing values occur and how many are present. R provides simple built-in functions like is.na(), which() and sum() to handle this task.

Consider a small data frame containing player statistics, where some values are missing:

Output:

👁 dataframe
Missing values

Functions Used

which(is.na(data))
sum(is.na(data))

Parameters:

  • is.na(data): Identifies missing values and returns TRUE for each NA.
  • which(is.na(data)): Returns the index positions of missing values.
  • sum(is.na(data)): Calculates the total number of missing values.

Find and Count Missing Values in the Entire Data Frame

We create a data frame named stats and use which(is.na()) to get the positions of missing values and sum(is.na()) to get the total number.

  • data.frame: creates tabular data from vectors.
  • is.na: checks whether a value is missing (NA).
  • which: returns the positions of TRUE values in a logical vector.
  • sum: counts TRUE values by summing them (as TRUE = 1, FALSE = 0).

Output:

👁 missing
Output

Count Missing Values Using summary()

We use summary() to get statistical details of each column, including the number of missing values.

  • summary: gives descriptive statistics and NA counts per column.

Output:

👁 players
Output

Count Missing Values Using colSums()

We use colSums() with is.na() to count NA values in each column.

  • colSums: computes the sum of each column, here to count NAs.
  • is.na: checks for missing values.

Output:

👁 players
Output

Find and Count Missing Values in a Single Column

We check the missing values in specific columns using dataframe$column.

  • $ operator: accesses a specific column from a data frame.

Output:

👁 data
Output

Find and Count Missing Values in All Columns

We use sapply() to apply functions column-wise and identify NA positions and counts.

  • sapply: applies a function to each column and returns a list or vector.
  • function(x): defines an anonymous function to check NAs per column.
  • which: returns positions of missing values.
  • sum: counts total missing values in each column.

Output:

👁 player_output
Output

The output shows the position and count of missing values in each column. The runs column has a missing value at position 4 and wickets has one at position 3, while player has no missing values. This helps quickly locate and quantify missing data column-wise.

Comment

Explore