Amitai Amitai - 9 days ago 5
R Question

Unelegant decorate-count-undecorate on data.table cumulative sum

I wish to keep a counter by "description".
Can the following code be 1-liner:

dt[, dummy:=1]
dt[, count:=lapply(.SD,cumsum), by = "description", .SDcols=("dummy")]
dt[, dummy:=NULL]

Answer

If I understand correctly, you just want:

dt[ , count := rowid(description)]