0

I have the following data frame:

df <-read.table(header=TRUE, text="id time Medication
                1 90 1 AA
                2 87 0 BB
                3 50 1 DD
                4 54 0 OX
                5 30 0 AR
                6 15 1 FM
                7 20 1 FM
                8 20 0 BB
                9 12 0 AA
                10 13 1 AA")

and a list that contains medications of interest:

medications <- c('AA', 'BB', 'FM')

I want to subset the data according to the medications available in medications. So far I have tried the following:

Medications_reduced <- df %>%
  subset(df$Medication %in% medications)
Economist_Ayahuasca
  • 1,648
  • 24
  • 33
  • 2
    `library(dplyr) Medications_reduced <- df %>% filter(Medication %in% medications)` – user63230 Aug 16 '21 at 15:41
  • 1
    There are a lot of questions on SO already about subsetting data based on a vector like this, and you already have some code. What about that code / research you've done on previous posts hasn't worked? – camille Aug 16 '21 at 16:22
  • 1
    Does this answer your question? [Select rows from a data frame based on values in a vector](https://stackoverflow.com/questions/11612235/select-rows-from-a-data-frame-based-on-values-in-a-vector) – camille Aug 16 '21 at 16:37
  • Hi @camille, yes pretty much. Do you want to create an answer to get the question answered!? – Economist_Ayahuasca Aug 17 '21 at 10:00
  • 1
    No, that was a vote to close this as a duplicate. If you agree with that vote, you should have an option to close it as such. It's generally much better to just close as duplicate than answer a question that's already been covered many times – camille Aug 17 '21 at 13:10

0 Answers0