I'm trying to use data.table to recode a factor variable to integers. Given:
group
 005j         
 005j         
 0k16         
 0fff       
 0fff
I'd like to get a series of increasing integers representing the same grouping:
group   intCode
 005j      1   
 005j      1   
 0k16      2   
 0fff      3 
 0fff      3
I could do this with a loop but it would be extremely slow.
You can simply use the .GRP symbol available in the j expression, with groups defined using by = group...
dt[ , intCode := .GRP , by = group ]
dt
#   group intCode
#1:  005j       1
#2:  005j       1
#3:  0k16       2
#4:  0fff       3
#5:  0fff       3
Quoting the help for ?data.table...
.GRPis aninteger,length1, containing a simple group counter. 1 for the 1st group, 2 for the 2nd, etc.
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