I'm trying to figure out a way to create an index corresponding to column values in an R data frame, e.g. for data frame
df = data.frame(c('x','x','y','y','y','z'), c(1,2,3,4,5,6),
c(7,8,9,10,11,12))
names(df) = c('a','b','c')
I want to create a new column (1,1,2,2,2,3) i.e. all instances of 'a' are replaced by 1, all of 'b' by 2, etc. My first thought was to convert df to a data table, for instance, I can use
dt = as.data.table(df)
dt[, id := seq_len(.N), by = 'a']
To create an id column (1,2,1,2,3,1). However, I don't know what to use in place of seq_len to give me the (1,1,2,2,2,3) indexing that I need.