0
votes

I am working with a data set that contains multiple observations for each prescription a patient is taking, with many different patients. Patients typically take one of several drugs, which are indicated as their own binary variables, Drug1, Drug2 and so on.

I am attempting to pull out only the individuals that have switched from one drug to the other, i.e, have a 1 in Drug1 column and Drug2, but these occur in different rows.

I have attempted to use newdata <- mydata[which(Drug1 == 1 & Drug2 == 1),] however, this assumes that the 1's are in the same row, which they are not.

Is there a way to select the patients that have received both drugs, but the indicator variables are in different rows?

Thank you

2

2 Answers

1
votes

I believe this is a solution to what you are asking using dplyr.

data <- data.frame(id = rep(c(1, 2, 3, 4), each = 2),
               drug1 = c(1, 0, 0, 0, 0, 1, 1, 1),
               drug2 = c(0, 1, 1, 1, 1, 0, 0, 0)
               )
library(dplyr)
data %>%
  group_by(id) %>%
  mutate(both_drugs = ifelse(any(drug1 == 1)  & any(drug2 == 1), 1, 0)) %>%
  filter(both_drugs == 1)
0
votes

Try creating a variable for each drug that indicates whether or not it was the only drug taken at that time by that individual.

data <- data.frame(id = rep(c(1, 2, 3, 4), each = 3),
                   drug1 = c(1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0),
                   drug2 = c(0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0))

library(dplyr)

data %>%
  group_by(id) %>%
  mutate(drug1only = ifelse(drug1==1 & drug2==0, 1, 0),
         drug2only = ifelse(drug2==1 & drug1==0, 1, 0)) %>%
  summarise(
    drug_switch = ifelse(max(drug1only)+max(drug2only)==2,1,0))