Using data.table with data structure something like:

library(data.table)
set.seed(12345)
dt <- data.table(id = c(rep('A',6),rep('B',3),rep('C',5),'D'), 
             day = c(rep(11:15,3)),
             drug = sample(LETTERS, 15, replace = TRUE))
setkey(dt,'id','day')
… 158 more words