I would like to generate sequence within subgroup columns e.g. I have two columns id1,val and would like to sort data by id1, val but then generate counter for id1.
Input
input <- data.frame("id1"=c(1,1,1,1,2,2,2),val=c(2,3,4,1,4,3,5))
Expected Output
id1,val,grp
1,1,1
1,2,2
1,3,3
1,4,4
2,3,1
2,4,2
2,5,3
Previous Reference Posts :
Count for sub group using .grp in data.table
Numbering rows within groups in a data frame
Used below code (I am trying to use code on big data and looking for a solution so I don't need to add an extra step to sort data for "val" column before generating sequence)
input[, new1:=seq_len(.N), by=c('id1')]