library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(readr)
unique_categories <- read_csv("bike folder/bike_orderlines.csv")
## Rows: 15644 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): model, category_1, category_2, frame_material, bikeshop_name, city...
## dbl  (5): order_id, order_line, quantity, price, total_price
## dttm (1): order_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
##Q1
unique_categories <- read_csv("bike folder/bike_orderlines.csv")
## Rows: 15644 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): model, category_1, category_2, frame_material, bikeshop_name, city...
## dbl  (5): order_id, order_line, quantity, price, total_price
## dttm (1): order_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
unique_categories %>%
  select(category_1) %>%
  distinct()
## # A tibble: 2 × 1
##   category_1
##   <chr>     
## 1 Mountain  
## 2 Road
unique_categories %>%
  select(category_2) %>%
  distinct()
## # A tibble: 9 × 1
##   category_2        
##   <chr>             
## 1 Over Mountain     
## 2 Trail             
## 3 Elite Road        
## 4 Endurance Road    
## 5 Sport             
## 6 Cross Country Race
## 7 Cyclocross        
## 8 Triathalon        
## 9 Fat Bike
unique_categories %>%
  select(frame_material) %>%
  distinct()
## # A tibble: 2 × 1
##   frame_material
##   <chr>         
## 1 Carbon        
## 2 Aluminum
##Q2
unique_categories <- read_csv("bike folder/bike_orderlines.csv")
## Rows: 15644 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): model, category_1, category_2, frame_material, bikeshop_name, city...
## dbl  (5): order_id, order_line, quantity, price, total_price
## dttm (1): order_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sales_by_category <- unique_categories

sales_by_category %>%
  group_by(category_1) %>%
  summarise(Sales = sum(total_price)) %>%
  rename(`Primary Category` = category_1) %>%
  arrange(desc(Sales))
## # A tibble: 2 × 2
##   `Primary Category`    Sales
##   <chr>                 <dbl>
## 1 Mountain           39154735
## 2 Road               31877595
sales_by_category %>%
  group_by(category_2) %>%
  summarise(Sales = sum(total_price)) %>%
  rename(`Secondary Category` = category_2) %>%
  arrange(desc(Sales))
## # A tibble: 9 × 2
##   `Secondary Category`    Sales
##   <chr>                   <dbl>
## 1 Cross Country Race   19224630
## 2 Elite Road           15334665
## 3 Endurance Road       10381060
## 4 Trail                 9373460
## 5 Over Mountain         7571270
## 6 Triathalon            4053750
## 7 Cyclocross            2108120
## 8 Sport                 1932755
## 9 Fat Bike              1052620
sales_by_category %>%
  group_by(frame_material) %>%
  summarise(Sales = sum(total_price)) %>%
  rename("Frame Materia" = frame_material) %>%
  arrange(desc(Sales))
## # A tibble: 2 × 2
##   `Frame Materia`    Sales
##   <chr>              <dbl>
## 1 Carbon          52940540
## 2 Aluminum        18091790
##Q3
unique_categories <- read_csv("bike folder/bike_orderlines.csv")
## Rows: 15644 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): model, category_1, category_2, frame_material, bikeshop_name, city...
## dbl  (5): order_id, order_line, quantity, price, total_price
## dttm (1): order_date
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
bike_orderlines <- unique_categories %>%
  rename(Primary_Category = category_1,
         Secondary_Category = category_2,
         Frame_Material = frame_material)

combinations_summary <- bike_orderlines %>%
  group_by(Primary_Category, Secondary_Category) %>%
  summarise(Aluminum = sum(ifelse(Frame_Material == "Aluminum", total_price, 0)),
            Carbon = sum(ifelse(Frame_Material == "Carbon", total_price, 0)),
            `Total Sales` = sum(total_price))
## `summarise()` has grouped output by 'Primary_Category'. You can override using
## the `.groups` argument.
print(combinations_summary)
## # A tibble: 9 × 5
## # Groups:   Primary_Category [2]
##   Primary_Category Secondary_Category Aluminum   Carbon `Total Sales`
##   <chr>            <chr>                 <dbl>    <dbl>         <dbl>
## 1 Mountain         Cross Country Race  3318560 15906070      19224630
## 2 Mountain         Fat Bike            1052620        0       1052620
## 3 Mountain         Over Mountain             0  7571270       7571270
## 4 Mountain         Sport               1932755        0       1932755
## 5 Mountain         Trail               4537610  4835850       9373460
## 6 Road             Cyclocross                0  2108120       2108120
## 7 Road             Elite Road          5637795  9696870      15334665
## 8 Road             Endurance Road      1612450  8768610      10381060
## 9 Road             Triathalon                0  4053750       4053750