I don't know why the summarize function did not work.
and I would like it to be like this:
Any help would be greatly appreciated!
Thank you!
CodePudding user response:
Two issues. First, you need to join the orders
and types
data together.
Second, summarize
requires a summary function, such as sum
. Your code is dividing by group but on a per-row basis, so the summary is getting repeated.
So something like:
orders %>%
left_join(types,
by = c("Product" = "Cookie Type")) %>%
mutate(cost = `Cost Per Cookie` * `Units Sold`,
rev = `Revenue Per Cookie` * `Units Sold`,
prof = rev - cost) %>%
group_by(Product) %>%
summarize(percent = (sum(prof) / sum(rev)) * 100)
Result:
# A tibble: 6 × 2
Product percent
<chr> <dbl>
1 Chocolate Chip 60
2 Fortune Cookie 50
3 Oatmeal Raisin 56
4 Snickerdoodle 62.5
5 Sugar 58.3
6 White Chocolate Macadamia Nut 54.2