I am agggregating and summarizing some multivariate data, using dplyr and tidyr. How do I present the data in a table-like form like below?
Data set:
year, division, group, count
2016, utensils, forks, 10
2016, utensils, spoons, 5
2016, utensils, knives, 20
2015, utensils, spoons, 4
2015, utensils, knives, 15
2015, utensils, forks, 11
2016, tools, hammer, 10
2016, tools, wrench, 5
2016, tools, awe, 20
2015, tools, hammer, 4
2015, tools, wrench, 15
2015, tools, awe 11
I would like to present the information like this:
2016 2015
Utensils Utensils
Forks count count
Spoons count count
Knives count count
2016 2015
Tools Tools
Hammer count count
Wrench count count
Awe count count
You can check this. Basically it is a reshape problem, but you need to split your data frame firstly by division column and then use dcast to transform each subset:
Or since each sub data frame contains only one unique division, you can drop it from the column names without adding it the dcast formula as it doesn't add extra information: