# How to remove rows from data frame in R based on grouping value of a particular column?

R ProgrammingServer Side ProgrammingProgramming

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

Live Demo

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 −

## 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 −

## 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 −

## 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
Published on 19-Oct-2020 14:32:07