I have a square matrix that is like:
I tried to first combine the rows with the same “Codes” number, but I was having a lot of trouble.
One way in base R:
Note that this can be simplified as denoted by @thelatemail
If you have better answer, please add a comment about this, thank you!