By what you are describing, if your index is not yet filled, use this:
dt2[, index := min(dates[grepl("^C10", drugs)], na.rm=TRUE), by=names]
If your index already has the correct value, and you are simply trying to fill the NA's, use the following instead, as it will be faster
> dt2[, index := index[!is.na(index)][[1]], by=names]
> dt2
names drugs dates index
1: mary C10AA07 2009-10-01 2010-07-01
2: mary C09AA03 2010-06-01 2010-07-01
3: mary C10AA07 2010-07-01 2010-07-01
4: mary A02BC01 2010-07-01 2010-07-01
5: mary C10AA07 2010-07-24 2010-07-01
6: tom C10AA05 2009-12-01 2010-04-06
7: tom C10AA05 2010-04-06 2010-04-06
8: tom C07AB03 2010-05-12 2010-04-06
9: tom C10AA05 2010-08-01 2010-04-06
>
If you are going to be doing this often, I would recommend setting key to drugs
or even creating a new column with the drugid. Note that you can use the key in .SD
, so the following would work for you:
dt2[, drugid := substr(drugs, 1, 3)]
setkey(dt2, drugid)
## HAVE A LOOK AT THE OUTPUT
dt2[, .SD[.("C10"), min(dates)]]
dt2[, .SD[.("C10"), min(dates)], by=names]
dt2[, .SD[.("C10"), min(dates)]$V1, by=names]
dt2[, index := .SD[.("C10"), min(dates)]$V1, by=names]