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:
df
identifier unit
ABCD-CH-4752-01X 27
ABCD-CH-4752-11X 15
ABCD-ER-9987-01X 27
ABCD-ER-9987-11X 15
CodePudding user response:
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))