[R] data.table: “group counter” with NAs
Marine Regis
marine.regis at hotmail.fr
Thu Jun 30 00:32:26 CEST 2016
Hello,
I would like to add a counter column in a data frame based on a set of identical rows. To do this, I tested:
DF = data.table(x=c("a","a","a","b","c","d","e","f","f"), y=c(1,3,2,8,8,4,NA,NA,NA))
DF[ , Index := .GRP, by = c("y") ]
DF
However, the rows with NAs are considered to be identical.
So, how can I obtain:
x y Index
1: a 1 1
2: a 3 2
3: a 2 3
4: b 8 4
5: c 8 4
6: d 4 5
7: e NA 6
8: f NA 7
9: f NA 8
Instead of:
x y Index
1: a 1 1
2: a 3 2
3: a 2 3
4: b 8 4
5: c 8 4
6: d 4 5
7: e NA 6
8: f NA 6
9: f NA 6
Thank you very much for your time.
Have a nice day
Marine
[[alternative HTML version deleted]]
More information about the R-help
mailing list