S Das
S Das

Reputation: 3391

Filter out a particular category

Here's a sample dataset.

a <- structure(list(ID = c("A1", "A2", "A3", "A1", "A1", "A2", "A4", "A5", "A2", "A3"), 
Type = c("A", "B", "C", "A", "A", "A", "B", "B", "C", "B"), 
Alc = c("E", "F", "G", "E", "E", "E", "F", "F", "F", "F"), 
Com = c("Y", "N", "Y", "N", "Y", "Y", "Y", "N", "N", "Y")),
.Names = c("ID", "Type", "Alc", "Com"), row.names = c(NA, -10L), class = "data.frame")
a
   ID Type Alc Com
1  A1    A   E   Y
2  A2    B   F   N
3  A3    C   G   Y
4  A1    A   E   N
5  A1    A   E   Y
6  A2    A   E   Y
7  A4    B   F   Y
8  A5    B   F   N
9  A2    C   F   N
10 A3    B   F   Y

I want to get a dataset having no "E" in Alc. I do the following.

library(dplyr)
b <- filter(a, Alc=="G"| Alc=="F")
b
  ID Type Alc Com
1 A2    B   F   N
2 A3    C   G   Y
3 A4    B   F   Y
4 A5    B   F   N
5 A2    C   F   N
6 A3    B   F   Y

If there are lots of categories in Alc, it's troublesome to write down all categories. I need an easy fix.

Thanks for your help.

Upvotes: 0

Views: 4104

Answers (3)

Rusan Kax
Rusan Kax

Reputation: 1894

Try subset(a,Alc $in$ c("G","F")), which is a common way to manipulate data frame objects, along with the standard subsetting bracket function [ ]. Look also at the drop argument of ?subset.

Upvotes: 0

rnso
rnso

Reputation: 24535

Try:

a[a$Alc!='E',]
   ID Type Alc Com
2  A2    B   F   N
3  A3    C   G   Y
7  A4    B   F   Y
8  A5    B   F   N
9  A2    C   F   N
10 A3    B   F   Y

Upvotes: 1

Mike.Gahan
Mike.Gahan

Reputation: 4615

You can use the "not equal" operator !=

b <- filter(a, Alc!="E")

Upvotes: 4

Related Questions