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)?
CodePudding user response:
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'))