adding new row based on criteria of existing rows

    # fix the time column, it should be numeric
    dtfrm[, "time"] <- as.numeric(as.character(dtfrm[, "time"]))

    library(data.table)
    DT <- data.table(cbind(dtfrm, rowid=seq(nrow(dtfrm))), key="id")

    # identify which rows need modification
    DT[, needsMod := FALSE]
    DT[unique(DT[, "id", with=FALSE])
       ,  needsMod := {L <- length(action); (action[L] == "l" && time[L] > 60)  }
       , by=id
       , mult="last"]

    # append new rows
    DT <- setkey(rbind(DT, 
           DT[c(needsMod), list(id, action, time=time-60, rowid=rowid+1e-2, needsMod=!needsMod)]), 
           id, rowid)

    # modify the identified rows
    DT[c(needsMod), c("action", "time") := list("e", 60)]

    # optionally remove added columns, though personally, I would keep some form of rowid
    DT[ , c("needsMod", "rowid") := NULL]
    DT

    #      id action time
    # 1: 12_1      l   15
    # 2: 12_1      d   45
    # 3: 12_1      e   60
    # 4: 12_1      l   30
    # 5: 12_2      l   20
    # 6: 12_2      d   30
    # 7: 12_2      e   60
    # 8: 12_2      l    1
