Open In App

Count the number of NA values in a DataFrame column in R

A null value in R is specified using either NaN or NA. In this article, we will see how can we count these values in a column of a dataframe.

Approach



Syntax: is.na(column)

Parameter:



column: column to be searched for na values

Returns:

A vector with boolean values, TRUE for NA otherwise FALSE

Given below are few examples

Example 1:




df<-data.frame(x = c(1,2,NA), y = rep(NA, 3))
print("dataframe is ")
print(df)
  
print("vector is")
vec = is.na(df[,1])
print(vec)
  
count = sum(vec)
  
print("count of NA in first column is" )
print(count)

Output:

Example 2:




df<-data.frame(x = c("kapil","rahul",NA,NA), y = c(1,2,NA,3))
print("dataframe is ")
print(df)
  
print("vector is")
vec = is.na(df[,1])
print(vec)
  
count = sum(vec)
  
print("count of NA in first column is" )
print(count)

Output:


Article Tags :