How to find the percentage of missing values in a dataframe in R?

07 Apr, 2021
In this article, let’s discuss how to find the percentage of missing values (NAs) in R Programming Language. Percentage of NAs denote the fraction of data cells that are not defined by a definite cell value. The percentage of NA values can be calculated using the following formula :
Percentage of NAs = (Number of cells with NA) * 100 /(Total number of cells)
Method 1: The total number of cells can be found by using the product of the inbuilt dim() function in R, which returns two values, each indicating the number of rows and columns respectively. 
The number of cells with NA values can be computed by using the sum() and is.na() functions in R respectively. The following code snippet first evaluates each data cell value to return a logical value of true if there is a missing value and false, if not. Then, the summation of these NA values is done using sum() function. 
sum(is.na(data_frame))
R
# declaring a data frame in Rdata_frame = data.frame(C1= c(1, 2, NA, 0),                        C2= c( NA, NA, 3, 8),                        C3= c("A", "V", "j", "y"))  print("Original data frame")print(data_frame)  # calculating the product of dimensions of dataframe totalcells = prod(dim(data_frame))print("Total number of cells ")print(totalcells)  # calculating the number of cells with namissingcells = sum(is.na(data_frame))print("Missing value cells")print(missingcells)  # calculating percentage of missing valuespercentage = (missingcells * 100 )/(totalcells)print("Percentage of missing values' cells")print (percentage)
Output
[1] "Original data frame"
 C1 C2 C3
1  1 NA  A
2  2 NA  V
3 NA  3  j
4  0  8  y
[1] "Total number of cells "
[1] 12
[1] "Missing value cells"
[1] 3
[1] "Percentage of missing values' cells"
[1] 25
Method 2: We can simply use the mean() function in R, to carry out the division of missing cells by the total number of cells. is.na() function is first used to determine whether the data cell value is true or false and then the mean() method is applied over it. The time complexity required is polynomial with respect to the size of data frame, since each data cell value is evaluated. 
R
# declaring a data frame in Rdata_frame = data.frame(C1= c(1, 2, NA, 0),                        C2= c( NA, NA, 3, 8),                         C3= c("A", "V", "j", "y"),                        C4=c(NA,NA,NA,NA))  print("Original data frame")print(data_frame)  # calculating percentage of missing valuespercentage = mean(is.na(data_frame)) * 100print ("percentage of missing values")print (percentage)
Output
[1] "Original data frame"
 C1 C2 C3 C4
1  1 NA  A NA
2  2 NA  V NA
3 NA  3  j NA
4  0  8  y NA
[1] "percentage of missing values"
[1] 43.75
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
How to Split Column Into Multiple Columns in R DataFrame?
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
R - if statement
How to Split Column Into Multiple Columns in R DataFrame?
How to filter R DataFrame by values in a column?
Replace Specific Characters in String in R
Merge DataFrames by Column Names in R
How to Sort a DataFrame in R ?