I am trying to understand if patients at a nursing home who are currently taking a series of drugs had any other drugs between a drugs certain drug of interest and death.The drug of interest is ibuprofen
id <- c('1', '1', '1','1','2','2','2')
drug <- c('ibuprofen','aleve','tylenol','claritin', 'ibuprofen','aleve','tums')
start <- c('100','105','50','115','20','-9','30')
death <- c('120','120','120','120','50','50','50')
The table of results I am expecting to see shows the drugs that were taken after ibuprofen and before death
id <- c('1','1','2')
drug <- c('aleve','claritin','tums')
start <- c('105','115','30)
So far this is the code I have, I have only filtered out to show patients with the drug wanted. But I am stuck on how to move forward.
y <- df %>%
filter(drug == "ibuprofen")
A one-liner!:
dplyr::filter(df, start > start[which(drug == 'ibuprofen')], .by = id)
Explanation:
which()
gets the index where something is true, in this case, the row where the drug is ibuprofen.
We can then use that to find the start value of the ibuprofen row, and then check for each row, if the start is after the ibuprofen start.