Let's say I have a table of purchases in a long format. It looks something like:
purchases = data.frame(
Item = c("Bike", "Bike", "Bike", "Bike", "Car", "Car", "Car", "Car"),
Variable = c("Age", "Age", "Price", "Price", "Age", "Age", "Price", "Price"),
Value = c("New", "Used", "Full", "Discount", "New", "Used", "Discount", "Discount")
)
I want to see the distribution of Value grouped by Item and Variable. So I could say "Of all Bikes sold, 50% were used" or "All Cars were sold at a discount."
The ideal output would be a table that looks like this:
I can get the count in dplyr doing something like this:
purchases %>% group_by(Item, Variable, Value) %>%
summarise(Total = n())
I would then went to divide each of those values by their respective groupings of just Item and Variable. I can think of some long answers where I conditionally add corresponding counts in another variable, but I was hoping to find an easy way to do it through dplyr. Another way to describe it might be performing calculations on one level up of a grouping.
