I have a data frame of counts per person that looks like this:
Person_ID Apple Pear Chicken Steak Spinach
1 1 0 0 5 1
2 1 1 1 0 0
3 0 0 0 3 2
I have another dataframe that maps which food belongs to which food group and looks like this:
Food Group
Apple Fruit
Pear Fruit
Chicken Meat
Steak Meat
Spinach Vegetable
I want to use the 2nd dataframe to add new columns on the 1st, basically creating new columns representing the food groups and collecting the counts based on the sum of their constituent columns, so that the final output looks like this:
Person_ID Apple Pear Chicken Steak Spinach Fruit Meat Vegetable
1 1 0 0 5 1 1 5 1
2 1 1 1 0 0 2 1 0
3 0 0 0 3 2 0 3 2
I am having trouble doing this in a clean way, and it seems quite complicated. I am wondering if there is a simple solution, and would appreciate advice on any solution at all