I have a data set that shows the number of products for each group and shop.
df <- tribble(
~shop_id, ~group, ~group_2, ~products,
'1', 'A', 'Z', 10,
'2', 'B', 'Y', 20,
'3', 'A', 'X', 30,
'4', 'B', 'X', 40,
'5', 'A', 'R', 10
)
I now want to see the share of products for each shop id and group. But I want to exclude the group 2 column in the data. For instance, there are 50 products in group A, so the share for shop 1 should be 0.2. Here is the desired output:
df <- tribble(
~shop_id, ~group, ~products, ~share_products,
'1', 'A', 10, 0.2,
'2', 'B', 20, 0.33,
'3', 'A', 30, 0.6,
'4', 'B', 40, 0.66,
'5', 'A', 10, 0.2
)
How can I do this?