# How to find the percentage of zeros in each column of a data frame in R?

To find the percentage of zeros in each column of a data frame in R, we can follow the below steps −

• First of all, create a data frame.

• Then, use colSums function along with nrow function to find the percentage of zeros in each column.

## Example 1

#### Create the data frame

Let’s create a data frame as shown below −

x<-rpois(25,2)
y<-rpois(25,2)
z<-rpois(25,2)
df1<-data.frame(x,y,z)
df1

## Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

   x y z
1  1 1 4
2  1 5 3
3  1 3 1
4  3 2 1
5  2 1 2
6  8 2 3
7  0 5 1
8  5 4 4
9  3 0 3
10 2 0 0
11 2 0 4
12 2 0 2
13 3 3 0
14 1 1 1
15 1 1 2
16 0 1 1
17 2 0 2
18 1 2 0
19 0 2 0
20 1 5 2
21 3 1 0
22 3 3 2
23 0 3 1
24 4 2 0
25 3 1 5

Find the percentage of zeros

Using colSums function along with nrow function to find the percentage of zeros in each column of data frame df1 −

x<-rpois(25,2)
y<-rpois(25,2)
z<-rpois(25,2)
df1<-data.frame(x,y,z)
(colSums(df1==0)/nrow(df1))*100

## Output

 x  y z
16 20 24

## Example 2

#### Create the data frame

Let’s create a data frame as shown below −

var1<-round(rnorm(25),0)
var2<-round(rnorm(25),0)
var3<-round(rnorm(25),0)
var4<-round(rnorm(25),0)
df2<-data.frame(var1,var2,var3,var4)
df2

## Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

  var1 var2 var3 var4
1  -1   1    0    0
2   1  -3    0   -2
3  -1   0    0    1
4   0  -1    0    0
5   1  -1   -1    0
6   1  -1   -1   -1
7  -1  -1    1    0
8   0   0    0    0
9   2   2   -1    1
10 -1  -2   -2   -1
11  1   1    1    0
12 -1   2    0    2
13  2   0   -1   -1
14  0  -1    0    1
15 -1   0   -1    0
16  0   1   -1   -1
17  0   0   -1    0
18 -1   0    0    1
19  0   1    1    0
20  1   1   -1   -1
21  0  -1    0    0
22  0   1    0    0
23 -1  -2   -1    1
24  1  -1    0    0
25  0   0    1    0

Find the percentage of zeros

Using colSums function along with nrow function to find the percentage of zeros in each column of data frame df2 −

var1<-round(rnorm(25),0)
var2<-round(rnorm(25),0)
var3<-round(rnorm(25),0)
var4<-round(rnorm(25),0)
df2<-data.frame(var1,var2,var3,var4)
(colSums(df2==0)/nrow(df2))*100

## Output

var1 var2 var3 var4
36  44    40   28