Find and count missing values in all columns in Data Frame

We can also find the missing values in the data frame column-wise. It reduces the complexity of searching for missing values in the data frame. Let’s look into a sample example program for finding and counting the missing values column-wise.

R




# create a data frame
stats <- data.frame(player=c('A', 'B', 'C', 'D'),
                 runs=c(100, 200, 408, NA),
                 wickets=c(17, 20, NA, 5))
 
# find location of missing values column wise
print("Position of missing values by column wise")
sapply(stats, function(x) which(is.na(x)))
 
# count the missing values by column wise
print("Count of missing values by column wise")
sapply(stats, function(x) sum(is.na(x)))


Output

"Position of missing values by column wise"
$player
integer(0)
$runs
4
$wickets
3
"Count of missing values by column wise"
player runs wickets
0 1 1

In this code, we will find the position and count of missing values in all the given columns in the dataframe. In order to find the missing values in all columns use apply function with the which and the sum function in is.na() method.

From the output, we can say that-

  • player column has no missing values.
  • runs column has 1 missing value at 4th position.
  • wickets column has 1 missing value at 3rd position.


How to Find and Count Missing Values in R DataFrame

In this article, we will be discussing how to find and count missing values in the R programming language.

Similar Reads

Find and Count Missing Values in the R DataFrame

Generally, missing values in the given data are represented with NA. In R programming, the missing values can be determined by is.na() method....

Find and count the Missing values From the entire Data Frame

In order to find the location of missing values and their count from the entire data frame pass the data frame name to the is.na() method. Let’s look into a program for finding and counting the missing values from the entire Data Frame....

Find and count the Missing values in one column of a Data Frame

...

Find and count missing values in all columns in Data Frame

...

Contact Us