How to subset a data frame by excluding a specific text value in an R data frame?


To create a subset based on text value we can use rowSums function by defining the sums for the text equal to zero, this will help us to drop all the rows that contains that specific text value. For example, if we have a data frame df that contains A in many columns then all the rows of df excluding A can be selected as−

df[rowSums(df=="A")==0,,drop=FALSE]

Example

 Live Demo

Consider the below data frame −

set.seed(951)
x1<−sample(LETTERS[1:3],20,replace=TRUE)
x2<−sample(LETTERS[1:4],20,replace=TRUE)
x3<−sample(LETTERS[1:5],20,replace=TRUE)
x4<−sample(LETTERS[2:5],20,replace=TRUE)
x5<−sample(LETTERS[3:5],20,replace=TRUE)
df<−data.frame(x1,x2,x3,x4,x5)
df

Output

  x1 x2 x3 x4 x5
1  A  D  B  C C
2  B  D  D  D D
3  B  A  D  D D
4  B  D  C  D E
5  C  D  C  C C
6  A  D  C  D E
7  B  D  E  B E
8  A  D  E  D C
9  A  B  C  E E
10 C  B  C  B C
11 A  D  D  B D
12 B  C  B  D E
13 A  C  E  E D
14 C  A  D  C E
15 C  C  D  B D
16 A  C  A  D E
17 C  A  B  C E
18 A  A  E  E D
19 B  A  D  D C
20 B  D  C  D C

Subsetting rows that do not include A −

df[rowSums(df=="A")==0,,drop=FALSE]

Output

  x1 x2 x3 x4 x5
2 B  D  D  D  D
4 B  D  C  D  E
5 C  D  C  C  C
7 B  D  E  B  E
10 C B  C  B  C
12 B C  B  D  E
15 C C  D  B  D
20 B D  C  D  C

Subsetting rows that do not include B −

df[rowSums(df=="B")==0,,drop=FALSE]

Output

x1 x2 x3 x4 x5
5 C D C C C
6 A D C D E
8 A D E D C
13 A C E E D
14 C A D C E
16 A C A D E
18 A A E E D

Subsetting rows that do not include C −

df[rowSums(df=="C")==0,,drop=FALSE]

Output

x1 x2 x3 x4 x5
2 B D D D D
3 B A D D D
7 B D E B E
11 A D D B D
18 A A E E D

Subsetting rows that do not include D −

df[rowSums(df=="D")==0,,drop=FALSE]

Output

x1 x2 x3  x4  x5
9 A  B  C  E  E
10 C B  C  B  C
17 C A  B  C  E

Subsetting rows that do not include E −

df[rowSums(df=="E")==0,,drop=FALSE]

Output

 x1 x2 x3 x4 x5
1 A  D  B  C C
2 B  D  D  D D
3 B  A  D  D D
5 C  D  C  C C
10 C B  C  B C
11 A D  D  B D
15 C C  D  B D
19 B A  D  D C
20 B D  C  D C

Updated on: 06-Nov-2020

637 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements