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 (1)` <- read_rds('bike_orderlines (1).rds')
#Q1
`bike_orderlines (1)` %>% select(model) %>%
mutate(model = case_when(
model == "CAAD Disc Ultegra" ~"CAAD12 Disc Ultegra",
TRUE ~ model)) %>%
mutate(model = case_when(
model == "Syapse Carbon Tiagra" ~ "Synapse Carbon Tiagra",
TRUE ~ model)) %>%
mutate(model = case_when(
model == "Supersix Evo Hi-Mod Utegra" ~ "Supersix Evo Hi-Mod Ultegra",
TRUE ~ model))
## # A tibble: 15,644 × 1
## model
## <chr>
## 1 Jekyll Carbon 2
## 2 Trigger Carbon 2
## 3 Beast of the East 1
## 4 Trigger Carbon 2
## 5 Supersix Evo Hi-Mod Team
## 6 Jekyll Carbon 4
## 7 Supersix Evo Black Inc.
## 8 Supersix Evo Hi-Mod Dura Ace 2
## 9 Synapse Disc 105
## 10 Synapse Carbon Disc Ultegra D12
## # ℹ 15,634 more rows
#Q2
`bike_orderlines (1)` %>%
select(order_date, total_price) %>%
mutate(month = order_date %>% floor_date("month") %>% month(label = TRUE)) %>%
group_by(month) %>%
summarise(sales = sum(total_price)) %>%
ungroup() %>%
mutate(sales = scales::dollar(sales)) %>%
arrange(month) %>%
rename(
"Month" = month,
"Sales" = sales
)
## # 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
#Q3
`bike_orderlines (1)` %>%
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
`bike_orderlines (1)` %>%
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
`bike_orderlines (1)`%>%
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
#Q4
`bike_orderlines (1)` %>%
distinct(category_1, category_2, model, price)%>%
separate(col = model,
into = str_c("model_", 1:9),
sep = " ",
remove = FALSE,
fill = "right",
extra = "drop"
) %>%
#lets create a base model feature from column model
mutate(model_base = case_when(
#fix Scalpel 29
str_detect(str_to_lower(model_1), "scalpel") ~ str_c(model_1, model_2, sep =" "),
#fix Beast of the East
str_detect(str_to_lower(model_1), "beast") ~ str_c(model_1, model_2, model_3, model_4, sep =" "),
TRUE ~ model_1)) %>%
#mutate(model_tier = model %>% str_replace(model_base) %>%
#str_trim) %>%
#remove unnecessary columns
select(-(model_1:model_9)) %>%
#select(-matches("[0-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(mean_price)
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
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:
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.