How to delete all rows except those belonging to more than one group

Advertisements

Suppose I have a dataset "df" as below

How to delete all rows except those belonging to groups "b" and "d" (or those belonging to any combination of two or three groups)?

I know how to do it if I had to keep only one group, but cannot figure out how to keep more than one.

For example, with grep function I could do

df <- df[grep("b", df$group),]

But again, how to keep more than just one group (suppose via using grep)?

>Solution :

For selecting specific groups, %in% is more specific than grep()/grepl().

df[df$group %in% c('a', 'c'), ]

or

subset(df, group %in% c('a', 'c'))

Leave a ReplyCancel reply