How to find the percentage of missing values in an R data frame?

To find the percentage of missing values in an R data frame, we can use sum function with the prod function. For example, if we have a data frame called df that contains some missing values then the percentage of missing values can be calculated by using the command: (sum(is.na(df))/prod(dim(df)))*100

Example1

y1<−sample(c(NA,rpois(2,1)),20,replace=TRUE)
y2<−sample(c(NA,rpois(2,1)),20,replace=TRUE)
y3<−sample(c(NA,rpois(2,5)),20,replace=TRUE)
y4<−sample(c(NA,rpois(2,3)),20,replace=TRUE)
y5<−sample(c(NA,rpois(2,2)),20,replace=TRUE)
y6<−sample(c(NA,rpois(2,5)),20,replace=TRUE)
df2<−data.frame(y1,y2,y3,y4,y5,y6)
df2

Output

y1 y2 y3 y4 y5 y6
1 2 NA NA 2 NA 5
2 1 NA 9 NA 2 5
3 1 1 9 NA 3 5
4 NA 0 6 3 3 5
5 2 1 NA 3 2 NA
6 2 NA 6 NA 3 NA
7 NA 0 6 2 3 NA
8 2 NA NA 3 NA 5
9 1 1 9 NA 3 NA
10 2 NA NA 3 NA 2
11 NA 1 6 3 3 NA
12 2 NA 6 3 2 2
13 NA NA 6 2 2 NA
14 2 0 6 NA NA NA
15 2 0 6 3 NA 5
16 NA 1 9 3 2 2
17 2 NA 6 3 2 NA
18 2 1 6 3 NA 2
19 NA 1 NA NA 3 2
20 2 NA 6 3 NA 5

Finding the percentage of missing values in df2 −

Example

(sum(is.na(df2))/prod(dim(df2)))*100

Output

[1] 34.16667
Updated on: 2026-03-11T22:50:55+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements