# data
TYPE CODE SUM overall rank rank sec
old AB 93 7 2
old LI 220 6 1
new EE 500 3 1
new HH 1.4 11 5
new AB 23.3 9 3
new xxx 62.05 8 2
new LI 10.68 10 4
end EE 500 3 3
end HH 384.6 5 4
end AB 600 2 2
end xxx 1375.7 1 1
data[order(SUM,decreasing = T,),"overall rank":=1:.N][]
data[order(SUM,decreasing = T,),"rank sec":=1:.N,by=c("TYPE","CODE")][]
setDT(data)[order(SUM, decreasing = TRUE), rank_sec := seq_len(.N), .(TYPE)]
data
# TYPE CODE SUM overall rank rank_sec
# 1: old AB 93.00 7 2 2
# 2: old LI 220.00 6 1 1
# 3: new EE 500.00 3 1 1
# 4: new HH 1.40 11 5 5
# 5: new AB 23.30 9 3 3
# 6: new xxx 62.05 8 2 2
# 7: new LI 10.68 10 4 4
# 8: end EE 500.00 3 3 3
# 9: end HH 384.60 5 4 4
#10: end AB 600.00 2 2 2
#11: end xxx 1375.70 1 1 1