Data.Table method of doing the following:

Can you please turn this question into a reproducible example? That’s the best way to make sure everybody is on the same page about what the code is doing, and therefore helps you and your helpers get to an answer more quickly.