I have a dataframe as it follows
Id | Code | year
1 | ZZZ | 2016
1 | KKK | 2016
1 | A23 | 2018
2 | A01 | 2018
2 | KKK | 2016
2 | ddd | 2017
3 | KKK | 2016
3 | ZZZ | 2016
4 | A23 | 2018
4 | 000 | 2018
5 | 009 | 2018
What I need is a table where for each id the table has a count of how many codes has at least a duplicated value in the df, separated by year. This should be an example af the output based on the df shown above.
Id | 2016 | 2017 | 2018 |
1 | 2 | 0 | 1 |
2 | 1 | 0 | 0 |
3 | 2 | 0 | 0 |
4 | 0 | 0 | 1 |