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