I have this easy task to do, imagine I have this table :
var1
1: X
2: X
3: Y
4: X
5: Z
6: Y
And I want to create a new variable like this :
var1 nth
1: X 1
2: X 2
3: Y 1
4: X 3
5: Z 1
6: Y 2
In reality I have a lot of variables plenty of lines. So my actual solution is (which works well) :
dt[, flag := 1]
dt[, nth := frank(flag, ties.method = "random"), by = .(var1)]
but this is slow, and I'm not really interested by ranking.(frank has to sort and rank, and I need just a simple thing which is indexing).
I imagined this solution, but doesn't work :
dt[, pos := .I, by = .(var1)]
Do you have any fast and straightforward solution for this task ?
Thank you
dt[, nth := row.names(.SD), by = var1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With