- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
- Related Articles
- How to subset a data frame by excluding a column using dplyr in R?
- How to change a text value in an R data frame?
- How to subset an R data frame by ignoring a value in one of the columns?
- How to subset a data frame by excluding the column names that are stored in a vector in R?
- How to scale the R data frame by excluding a particular column?
- How to filter rows by excluding a particular value in columns of the R data frame?
- How to subset factor columns in an R data frame?
- How to calculate row means by excluding NA values in an R data frame?
- How to find the row sums by excluding a column in R data frame?
- How to subset nth row from an R data frame?
- Create a sample of data frame column by excluding NAs in R
- How to subset an R data frame by specifying columns that contains NA?
- How to create a subset for a factor level in an R data frame?
- How to subset an R data frame based on small letters?
- How to divide data frame row values by maximum value in each row excluding 0 in R?

Advertisements