How to filter out certain rows from a data frame

Hi there,

I have a data frame called drugs_study this has three columns called formulation_code, approved_name, and date. The formulation_code includes the type of formulation of rug (cream, tablet etc), the approved_name is the drug name (aspirin etc).

I would like to filter in(and exclude all other rows) all of some of the formulations from the formulation_code (for example all tablets) as well as some selected formulations for specific drug names (for example aspirin capsules). All the rest of the data frame should then be excluded from the final new tibble which is called drugs_filtered.

I have tried
drugs_filtered <-
drugs_study %>%
filter(
any(formulation_code == "TABLETS") |
any(formulation_code == "CAPSULES" & approved_name == "ASPIRIN")

But that doesn't actually seem to exclude all the other rows. I tried select() also but could not get that to work. Does anyone have any ideas?

Many thanks,

Clare

I think you wnt

drugs_filtered <-
drugs_study %>%
filter(
  formulation_code == "TABLETS" |
  (formulation_code == "CAPSULES" & approved_name == "ASPIRIN"))

Thank you so much, amazing how long it took me to work that out - really appreciate it!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.