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 filter for rows using multiple conditions for strings

How can I filter rows in df based on multiple conditions with strings? I want to extract all rows with the string ‘CH-4752’ OR ‘ER-9987’.

In other words, go from this:

df
identifier        unit

ABCD-CH-4752-01X  27  
ABCD-CH-4752-11X  15
ABCD-AZ-5155-01X  6
ABCD-ER-9987-01X  27  
ABCD-ER-9987-11X  15
ABCD-GH-5230-01X  72
ABCD-UI-9078-01X  9
ABCD-OP-7489-01X  88

to this:

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

df
identifier        unit

ABCD-CH-4752-01X  27  
ABCD-CH-4752-11X  15
ABCD-ER-9987-01X  27  
ABCD-ER-9987-11X  15

>Solution :

You can try this using grep

df[grep("CH-4752|ER-9987",df$identifier),]
        identifier unit
1 ABCD-CH-4752-01X   27
2 ABCD-CH-4752-11X   15
4 ABCD-ER-9987-01X   27
5 ABCD-ER-9987-11X   15

Data

df <- structure(list(identifier = c("ABCD-CH-4752-01X", "ABCD-CH-4752-11X",
"ABCD-AZ-5155-01X", "ABCD-ER-9987-01X", "ABCD-ER-9987-11X", "ABCD-GH-5230-01X",
"ABCD-UI-9078-01X", "ABCD-OP-7489-01X"), unit = c(27L, 15L, 6L,
27L, 15L, 72L, 9L, 88L)), class = "data.frame", row.names = c(NA,
-8L))
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