Imperative code in R keeping track of state
R encourages vector-based operations instead of loops. But in this case, I don't think there's a good way around tracking the state of delta. 

This might be a slightly better way to create a vector delta:

delta &lt;- 0
vdelta &lt;- c(0)
for (i in 2:nrow(db)) {
  row &lt;- db[i,]
  prev &lt;- db[i-1,]
  if (row$class != prev$class) {
    delta &lt;- 0
  } else if (row$src != prev$src) {
    delta &lt;- prev$end + delta
  }
  vdelta &lt;- c(vdelta, delta)
}


Based on vdelta you can easily create the columns you need:

db$corr.start &lt;- db$start + vdelta
db$corr.end &lt;- db$end + vdelta
db$delta &lt;- vdelta