R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(readr)
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(scales)
## 
## Attaching package: 'scales'
## The following object is masked from 'package:readr':
## 
##     col_factor
bike_orderlines <- read_csv('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.

Question 1

unique(`bike_orderlines`$category_1)
## [1] "Mountain" "Road"
unique(`bike_orderlines`$category_2)
## [1] "Over Mountain"      "Trail"              "Elite Road"        
## [4] "Endurance Road"     "Sport"              "Cross Country Race"
## [7] "Cyclocross"         "Triathalon"         "Fat Bike"
unique(`bike_orderlines`$frame_material)
## [1] "Carbon"   "Aluminum"

Question 2

bike_orderlines_rename <- bike_orderlines %>%
  rename('Primary Category' = category_1) %>%
  rename('Secondary Category' = category_2) %>%
  rename('Frame Material' = frame_material)

primary_category_sales <- bike_orderlines_rename %>% 
  select(`Primary Category`, total_price) %>% 
  group_by(`Primary Category`) %>% 
  summarise(Sales = sum(total_price)) %>% 
  arrange(desc(Sales)) %>%
  ungroup() %>% 
  mutate(Sales = dollar(Sales))
primary_category_sales
## # A tibble: 2 × 2
##   `Primary Category` Sales      
##   <chr>              <chr>      
## 1 Mountain           $39,154,735
## 2 Road               $31,877,595
secondary_category_sales <- bike_orderlines_rename %>% 
  select(`Secondary Category`, total_price) %>% 
  group_by(`Secondary Category`) %>% 
  summarise(Sales = sum(total_price)) %>% 
  arrange(desc(Sales)) %>%
  ungroup() %>% 
  mutate(Sales = dollar(Sales))
secondary_category_sales
## # A tibble: 9 × 2
##   `Secondary Category` Sales      
##   <chr>                <chr>      
## 1 Cross Country Race   $19,224,630
## 2 Elite Road           $15,334,665
## 3 Endurance Road       $10,381,060
## 4 Trail                $9,373,460 
## 5 Over Mountain        $7,571,270 
## 6 Triathalon           $4,053,750 
## 7 Cyclocross           $2,108,120 
## 8 Sport                $1,932,755 
## 9 Fat Bike             $1,052,620
frame_material_sales <- bike_orderlines_rename %>% 
  select(`Frame Material`, total_price) %>% 
  group_by(`Frame Material`) %>% 
  summarise(Sales = sum(total_price)) %>%
  arrange(desc(Sales)) %>%
  ungroup() %>% 
  mutate(Sales = dollar(Sales))
frame_material_sales
## # A tibble: 2 × 2
##   `Frame Material` Sales      
##   <chr>            <chr>      
## 1 Carbon           $52,940,540
## 2 Aluminum         $18,091,790

Question 3

bike_orderlines_rename %>% 
  mutate(Aluminum = ifelse(`Frame Material` == 'Aluminum', total_price, 0)) %>% 
  mutate(Carbon = ifelse(`Frame Material` == 'Carbon', total_price, 0)) %>% 
  select(`Primary Category`, `Secondary Category`, Aluminum, Carbon, total_price) %>% 
  group_by(`Primary Category`, `Secondary Category`) %>% 
  summarise(Aluminum = sum(Aluminum), Carbon = sum(Carbon), Sales = sum(total_price)) %>% 
  arrange(desc(Sales)) %>% 
  ungroup() %>% 
  mutate(Aluminum = dollar(Aluminum), Carbon = dollar(Carbon), Sales = dollar(Sales))
## `summarise()` has grouped output by 'Primary Category'. You can override using
## the `.groups` argument.
## # A tibble: 9 × 5
##   `Primary Category` `Secondary Category` Aluminum   Carbon      Sales      
##   <chr>              <chr>                <chr>      <chr>       <chr>      
## 1 Mountain           Cross Country Race   $3,318,560 $15,906,070 $19,224,630
## 2 Road               Elite Road           $5,637,795 $9,696,870  $15,334,665
## 3 Road               Endurance Road       $1,612,450 $8,768,610  $10,381,060
## 4 Mountain           Trail                $4,537,610 $4,835,850  $9,373,460 
## 5 Mountain           Over Mountain        $0         $7,571,270  $7,571,270 
## 6 Road               Triathalon           $0         $4,053,750  $4,053,750 
## 7 Road               Cyclocross           $0         $2,108,120  $2,108,120 
## 8 Mountain           Sport                $1,932,755 $0          $1,932,755 
## 9 Mountain           Fat Bike             $1,052,620 $0          $1,052,620

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.