Amitai Amitai - 1 year ago 72
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 Source

If I understand correctly, you just want:

dt[ , count := rowid(description)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download