- 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 remove rows from data frame in R based on grouping value of a particular column?
If we have a grouping column in an R data frame and we believe that one of the group values is not useful for our analysis then we might want to remove all the rows that contains that value and proceed with the analysis, also it might be possible that the one of the values are repeated and we want to get rid of that. In this situation, we can do subsetting of the data frame using negation and single square brackets.
Example
set.seed(1212) x<-sample(LETTERS[1:3],20,replace=TRUE) y<-rpois(20,5) df<-data.frame(x,y) df
Output
x y 1 A 6 2 A 3 3 C 4 4 B 4 5 B 8 6 A 5 7 A 9 8 A 6 9 C 5 10 A 7 11 B 3 12 C 4 13 A 4 14 B 7 15 B 2 16 B 5 17 A 5 18 A 5 19 C 7 20 A 5
Removing rows that contain A in column x −
Example
df[!df$x=='A',]
Output
x y 1 B 7 4 B 4 5 C 7 6 B 2 8 C 5 10 B 7 12 C 3 13 C 6 14 C 5 16 C 4 17 B 5 19 B 7
Removing rows that contain B in column x −
Example
df[!df$x=='B',]
Output
x y 2 A 3 3 A 4 5 C 7 7 A 5 8 C 5 9 A 5 11 A 5 12 C 3 13 C 6 14 C 5 15 A 4 16 C 4 18 A 2 20 A 11
Removing rows that contain C in column x −
Example
df[!df$x=='C',]
Output
x y 1 B 7 2 A 3 3 A 4 4 B 4 6 B 2 7 A 5 9 A 5 10 B 7 11 A 5 15 A 4 17 B 5 18 A 2 19 B 7 20 A 11
Removing rows that contain 3 in column y −
Example
df[!df$y==3,]
Output
x y 1 B 7 3 A 4 4 B 4 5 C 7 6 B 2 7 A 5 8 C 5 9 A 5 10 B 7 11 A 5 13 C 6 14 C 5 15 A 4 16 C 4 17 B 5 18 A 2 19 B 7 20 A 11
Removing rows that contain 7 in column y −
Example
df[!df$y==7,]
Output
x y 2 A 3 3 A 4 4 B 4 6 B 2 7 A 5 8 C 5 9 A 5 11 A 5 12 C 3 13 C 6 14 C 5 15 A 4 16 C 4 17 B 5 18 A 2 20 A 11
Removing rows that contain 5 in column y −
Example
df[!df$y==5,]
Output
x y 1 B 7 2 A 3 3 A 4 4 B 4 5 C 7 6 B 2 10 B 7 12 C 3 13 C 6 15 A 4 16 C 4 18 A 2 19 B 7 20 A 11
Advertisements