Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert factor variable of strings to list of integers using data.table

Tags:

r

data.table

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.

like image 678
Luke Avatar asked Dec 25 '22 17:12

Luke


1 Answers

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...

.GRP is an integer, length 1, containing a simple group counter. 1 for the 1st group, 2 for the 2nd, etc.

like image 192
Simon O'Hanlon Avatar answered Dec 28 '22 08:12

Simon O'Hanlon