Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

enter image description here

>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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading