Home > Blockchain >  Cannot summarize in R
Cannot summarize in R

Time:11-30

I don't know why the summarize function did not work.

Here is my dataset: enter image description here

and I would like it to be like this:

enter image description here

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
  • Related