peixe
peixe

Reputation: 1292

Drop several factor levels in data frame in R

I needed to drop several factor levels from a data frame in R. With the solution provided in this question, I can get rid of one of them, but... is it possible to remove several factor levels in one effort?

I came up with this piece of code, subsetting as many times as factors needed to remove...

dino <- read.csv('/home/maxim/onset.csv', header=TRUE)
dino <- subset(dino, onset != "QT")
dino <- subset(dino, onset != "")
table(droplevels(dino)$onset)

It works fine in my case, but i was wondering if anyone knows a more direct way to do it. (BTW, I'm not very profficient in R...)

Upvotes: 1

Views: 4208

Answers (2)

peixe
peixe

Reputation: 1292

Solution apported by @Matthew Plourde:

dino[! dino$onset %in% c('QT', ''), ]

Upvotes: 2

peixe
peixe

Reputation: 1292

Solution apported by @Joris Meys:

subset(dino, ! onset %in% c("QT",""))

Upvotes: 0

Related Questions