I have a table in long format as shown below, every row is unique in this input table :-
year variable
2014 ab
2014 cd
2014 ef
2016 ef
2016 gh
2014 ab
2014 cd
2014 ef
2016 ef
2016 gh
I would like convert this table into wide format but only for variable
column which looks like a contigency matrix. For example - As shown in the output table below, the combination of ab+cd appears ONCE for year 2014, and the combination of ab+ef also appears Once for year 2014. In that way, the first row of my output table clearly shows all the Counts
for different combinations variable
column of input table for different years.
year value ab cd ef gh
2014 ab 2 2 2 0
2014 cd 2 2 2 0
2014 ef 2 2 2 0
2014 ef 0 0 2 2
2016 gh 0 0 2 2
I have tried to reshape table quite a few times but have not been able to achieve the result that I want to have. I would very much appreciate if the solution includes use of data.table
. Thank you.