Suppose your data has been stored as "data.txt"
A few things to consider:
- adding evid to your dataset. I would be more comfortable to use evid == 1 | evid == 4 than dose > 0
- consider using NONMEM to generate time after first / last dose, so that you do not have to worry about the transform from string to date-time in R:
$INPUT ID DATE TIME CONC DOSE ...
$ERROR / $PRED
IF(NEWIND.LE.1.AND.DOSE.GT.0) TFDS=TIME ; TIME OF FIRST DOSE
TSFD=TIME-TFDS ; TIME SINCE FIRST DOSE
IF(DOSE.GT.0) TLDS=TIME ; TIME OF LAST DOSE
TSLD=TIME-TSLD; TIME SINCE LAST DOSE
...
$TABLE ... TSFD TSLD ...