How to remove rows that contains all zeros in an R data frame?

Often, we get missing data and sometimes missing data is filled with zeros if zero is not the actual range for a variable. In this type of situations, we can remove the rows where all the values are zero. For this purpose, we can use rowSums function and if the sum is greater than zero then keep the row otherwise neglect it.

Example1

Live Demo

Consider the below data frame −

set.seed(251)
x1<−sample(0:1,20,replace=TRUE)
x2<−sample(0:1,20,replace=TRUE)
df1<−data.frame(x1,x2)
df1

Output

x1 x2
1 0 0
2 0 1
3 0 1
4 1 1
5 1 0
6 0 1
7 1 0
8 1 1
9 1 1
10 0 0
11 0 1
12 1 0
13 1 0
14 1 1
15 0 1
16 1 1
17 1 1
18 1 1
19 1 1
20 0 0

Removing rows having all zeros −

df1[rowSums(df1[])>0,]


Output

x1 x2
2 0 1
3 0 1
4 1 1
5 1 0
6 0 1
7 1 0
8 1 1
9 1 1
11 0 1
12 1 0
13 1 0
14 1 1
15 0 1
16 1 1
17 1 1
18 1 1
19 1 1

Example2

Live Demo

y1<−sample(c(0,5),20,replace=TRUE)
y2<−sample(c(0,2),20,replace=TRUE)
df2<−data.frame(y1,y2)
df2

Output

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

Removing rows having all zeros −

df2[rowSums(df2[])>0,]


Output

y1 y2
1 5 0
2 0 2
3 0 2
5 5 2
6 5 0
7 5 0
8 0 2
9 0 2
10 5 0
13 5 0
15 5 2
16 5 0
18 0 2
19 5 2

Example3

Live Demo

z1<−rep(0,20)
z2<−sample(c(0,1),20,replace=TRUE)
df3<−data.frame(z1,z2)
df3

Output

z1 z2
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 1
7 0 1
8 0 1
9 0 1
10 0 1
11 0 1
12 0 0
13 0 0
14 0 1
15 0 1
16 0 0
17 0 1
18 0 1
19 0 1
20 0 1

Removing rows having all zeros −

df3[rowSums(df3[])>0,]


Output

z1 z2
6 0 1
7 0 1
8 0 1
9 0 1
10 0 1
11 0 1
14 0 1
15 0 1
17 0 1
18 0 1
19 0 1
20 0 1

Updated on: 07-Nov-2020

7K+ Views