简体   繁体   中英

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...)

@Matthew Plourde 提出的解决方案:

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

@Joris Meys 提出的解决方案:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM