R Markdown

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)

path2 = "./bike_orderlines.rds"
bike_orderlines <- read_rds(path2)

Question 1

Fix typos found in feature engineering

bike_orderlines %>% mutate(model = case_when(
        model == "CAAD Disc Ultegra" ~ "CAAD12 Disc Ultegra",
        model == "Syapse Carbon Tiagra" ~ "Synapse Carbon Tiagra",
        model == "Supersix Evo Hi-Mod Utegra" ~ "Supersix Evo Hi-Mod Ultegra",
        TRUE ~ model
    ))
## # A tibble: 15,644 × 13
##    order_date          order_id order_line quantity price total_price model     
##    <dttm>                 <dbl>      <dbl>    <dbl> <dbl>       <dbl> <chr>     
##  1 2011-01-07 00:00:00        1          1        1  6070        6070 Jekyll Ca…
##  2 2011-01-07 00:00:00        1          2        1  5970        5970 Trigger C…
##  3 2011-01-10 00:00:00        2          1        1  2770        2770 Beast of …
##  4 2011-01-10 00:00:00        2          2        1  5970        5970 Trigger C…
##  5 2011-01-10 00:00:00        3          1        1 10660       10660 Supersix …
##  6 2011-01-10 00:00:00        3          2        1  3200        3200 Jekyll Ca…
##  7 2011-01-10 00:00:00        3          3        1 12790       12790 Supersix …
##  8 2011-01-10 00:00:00        3          4        1  5330        5330 Supersix …
##  9 2011-01-10 00:00:00        3          5        1  1570        1570 Synapse D…
## 10 2011-01-11 00:00:00        4          1        1  4800        4800 Synapse C…
## # ℹ 15,634 more rows
## # ℹ 6 more variables: category_1 <chr>, category_2 <chr>, frame_material <chr>,
## #   bikeshop_name <chr>, city <chr>, state <chr>

Question 2

Which month has the biggest bike sales

bike_orderlines %>% 
  select(order_date, total_price) %>% 
  mutate(month = order_date %>% floor_date("month") %>% month(label =   TRUE)) %>%
  group_by(month) %>% 
  summarize(sales = sum(total_price)) %>%
  ungroup() %>%
  mutate(sales = scales::dollar(sales)) %>%
  arrange(month) 
## # A tibble: 12 × 2
##    month sales     
##    <ord> <chr>     
##  1 Jan   $4,089,460
##  2 Feb   $5,343,295
##  3 Mar   $7,282,280
##  4 Apr   $8,386,170
##  5 May   $7,935,055
##  6 Jun   $7,813,105
##  7 Jul   $7,602,005
##  8 Aug   $5,346,125
##  9 Sep   $5,556,055
## 10 Oct   $4,394,300
## 11 Nov   $4,169,755
## 12 Dec   $3,114,725

Question 3

Black Inc

bike_orderlines %>%
    select(model, total_price)%>%
    mutate(Black = model %>% str_detect("Black Inc") %>% as_factor())     %>%
    group_by(Black)%>%
    summarise(Median_Orderline = median(total_price))%>%
    ungroup() %>%
    mutate(Median_Orderline = scales::dollar(Median_Orderline))
## # A tibble: 2 × 2
##   Black Median_Orderline
##   <fct> <chr>           
## 1 FALSE $2,880          
## 2 TRUE  $12,250
### Ultegra
bike_orderlines %>%
    select(model, total_price)%>%
    mutate(Ultegra = model %>% str_detect("Ultegra") %>% as_factor())     %>%
    group_by(Ultegra)%>%
    summarise(Median_Orderline = median(total_price))%>%
    ungroup() %>%
    mutate(Median_Orderline = scales::dollar(Median_Orderline))
## # A tibble: 2 × 2
##   Ultegra Median_Orderline
##   <fct>   <chr>           
## 1 FALSE   $3,200          
## 2 TRUE    $3,200
### Disc
bike_orderlines %>%
    select(model, total_price)%>%
    mutate(Disc = model %>% str_detect("Disc") %>% as_factor()) %>%
    group_by(Disc)%>%
    summarise(Median_Orderline = median(total_price))%>%
    ungroup() %>%
    mutate(Median_Orderline = scales::dollar(Median_Orderline))
## # A tibble: 2 × 2
##   Disc  Median_Orderline
##   <fct> <chr>           
## 1 FALSE $3,200          
## 2 TRUE  $2,660

Question 4

bike_orderlines %>%
  distinct(category_1, category_2, model, price)%>%
  separate(col    = model,
           into   = str_c("model_", 1:9),
           sep    = " ",
           remove = FALSE,
           fill   = "right",
           extra  = "drop"
  ) %>%
  

  mutate(model_base = case_when(
    

    str_detect(str_to_lower(model_1), "scalpel") ~ str_c(model_1, model_2,  sep =" "),
  
    str_detect(str_to_lower(model_1), "beast") ~ str_c(model_1, model_2, model_3, model_4,  sep =" "),
    TRUE ~ model_1)) %>%
  

  
  select(-(model_1:model_9)) %>%
  group_by(category_1, category_2, model_base) %>%
  summarise(
    mean_price = mean(price),
    min_price  = min(price),
    max_price  = max(price)
  ) %>%
  ungroup() %>%
  mutate(
    mean_price = scales::dollar(mean_price, largest_with_cents = 1e+4),
    min_price  = scales::dollar(min_price, largest_with_cents = 1e+4),
    max_price  = scales::dollar(max_price, largest_with_cents = 1e+4)) %>%
  
  arrange(desc(mean_price)) 
## `summarise()` has grouped output by 'category_1', 'category_2'. You can
## override using the `.groups` argument.
## # A tibble: 23 × 6
##    category_1 category_2         model_base       mean_price min_price max_price
##    <chr>      <chr>              <chr>            <chr>      <chr>     <chr>    
##  1 Mountain   Cross Country Race Scalpel-Si Race  $9,060     $9,060    $9,060   
##  2 Mountain   Cross Country Race Scalpel-Si Hi-M… $7,460     $7,460    $7,460   
##  3 Mountain   Sport              Catalyst         $546       $415      $705     
##  4 Road       Elite Road         Supersix         $5,491     $1,840    $12,790  
##  5 Mountain   Cross Country Race Scalpel-Si Carb… $5,327     $4,260    $6,390   
##  6 Mountain   Over Mountain      Jekyll           $5,275     $3,200    $7,990   
##  7 Mountain   Over Mountain      Trigger          $5,275     $3,200    $8,200   
##  8 Mountain   Cross Country Race F-Si             $5,070     $1,840    $11,190  
##  9 Mountain   Trail              Habit            $5,052     $1,950    $12,250  
## 10 Mountain   Cross Country Race Scalpel 29       $4,795     $3,200    $6,390   
## # ℹ 13 more rows