# Load up libraries:pacman
library(readxl)
if (!require("pacman")) install.packages("pacman")
## Loading required package: pacman
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(tidyquant)
library(readxl)
# library(writexl)
# 2. Import data
#readxl::read_excel()

path1 = "./bike folder/bikes.xlsx"

read_excel(path1)
## # A tibble: 97 × 4
##    bike.id model                          description                price
##      <dbl> <chr>                          <chr>                      <dbl>
##  1       1 Supersix Evo Black Inc.        Road - Elite Road - Carbon 12790
##  2       2 Supersix Evo Hi-Mod Team       Road - Elite Road - Carbon 10660
##  3       3 Supersix Evo Hi-Mod Dura Ace 1 Road - Elite Road - Carbon  7990
##  4       4 Supersix Evo Hi-Mod Dura Ace 2 Road - Elite Road - Carbon  5330
##  5       5 Supersix Evo Hi-Mod Utegra     Road - Elite Road - Carbon  4260
##  6       6 Supersix Evo Red               Road - Elite Road - Carbon  3940
##  7       7 Supersix Evo Ultegra 3         Road - Elite Road - Carbon  3200
##  8       8 Supersix Evo Ultegra 4         Road - Elite Road - Carbon  2660
##  9       9 Supersix Evo 105               Road - Elite Road - Carbon  2240
## 10      10 Supersix Evo Tiagra            Road - Elite Road - Carbon  1840
## # ℹ 87 more rows
bikes_tbl <-read_excel(path1)
bikeshop_tbl <-read_excel(path = "./bike folder/bikeshops.xlsx")
orderline_tbl <-read_excel(path = "./bike folder/orderlines.xlsx")
## New names:
## • `` -> `...1`
bike_orderlines_tbl <-read_excel(path = "./bike folder/bike_orderlines.xlsx")
# 3. Examining Data----
bikes_tbl
## # A tibble: 97 × 4
##    bike.id model                          description                price
##      <dbl> <chr>                          <chr>                      <dbl>
##  1       1 Supersix Evo Black Inc.        Road - Elite Road - Carbon 12790
##  2       2 Supersix Evo Hi-Mod Team       Road - Elite Road - Carbon 10660
##  3       3 Supersix Evo Hi-Mod Dura Ace 1 Road - Elite Road - Carbon  7990
##  4       4 Supersix Evo Hi-Mod Dura Ace 2 Road - Elite Road - Carbon  5330
##  5       5 Supersix Evo Hi-Mod Utegra     Road - Elite Road - Carbon  4260
##  6       6 Supersix Evo Red               Road - Elite Road - Carbon  3940
##  7       7 Supersix Evo Ultegra 3         Road - Elite Road - Carbon  3200
##  8       8 Supersix Evo Ultegra 4         Road - Elite Road - Carbon  2660
##  9       9 Supersix Evo 105               Road - Elite Road - Carbon  2240
## 10      10 Supersix Evo Tiagra            Road - Elite Road - Carbon  1840
## # ℹ 87 more rows
str(bikes_tbl)
## tibble [97 × 4] (S3: tbl_df/tbl/data.frame)
##  $ bike.id    : num [1:97] 1 2 3 4 5 6 7 8 9 10 ...
##  $ model      : chr [1:97] "Supersix Evo Black Inc." "Supersix Evo Hi-Mod Team" "Supersix Evo Hi-Mod Dura Ace 1" "Supersix Evo Hi-Mod Dura Ace 2" ...
##  $ description: chr [1:97] "Road - Elite Road - Carbon" "Road - Elite Road - Carbon" "Road - Elite Road - Carbon" "Road - Elite Road - Carbon" ...
##  $ price      : num [1:97] 12790 10660 7990 5330 4260 ...
glimpse(bikes_tbl)
## Rows: 97
## Columns: 4
## $ bike.id     <dbl> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,…
## $ model       <chr> "Supersix Evo Black Inc.", "Supersix Evo Hi-Mod Team", "Su…
## $ description <chr> "Road - Elite Road - Carbon", "Road - Elite Road - Carbon"…
## $ price       <dbl> 12790, 10660, 7990, 5330, 4260, 3940, 3200, 2660, 2240, 18…
# 4. Select()
bike_orderlines_tbl %>%
      select(starts_with("order_"))
## # A tibble: 15,644 × 3
##    order_date          order_id order_line
##    <dttm>                 <dbl>      <dbl>
##  1 2011-01-07 00:00:00        1          1
##  2 2011-01-07 00:00:00        1          2
##  3 2011-01-10 00:00:00        2          1
##  4 2011-01-10 00:00:00        2          2
##  5 2011-01-10 00:00:00        3          1
##  6 2011-01-10 00:00:00        3          2
##  7 2011-01-10 00:00:00        3          3
##  8 2011-01-10 00:00:00        3          4
##  9 2011-01-10 00:00:00        3          5
## 10 2011-01-11 00:00:00        4          1
## # ℹ 15,634 more rows
# Reduce columns, rearrange columns
bike_orderlines_tbl %>%
  select(bikeshop_name:state, everything())
## # A tibble: 15,644 × 13
##    bikeshop_name    city  state order_date          order_id order_line quantity
##    <chr>            <chr> <chr> <dttm>                 <dbl>      <dbl>    <dbl>
##  1 Ithaca Mountain… Itha… NY    2011-01-07 00:00:00        1          1        1
##  2 Ithaca Mountain… Itha… NY    2011-01-07 00:00:00        1          2        1
##  3 Kansas City 29e… Kans… KS    2011-01-10 00:00:00        2          1        1
##  4 Kansas City 29e… Kans… KS    2011-01-10 00:00:00        2          2        1
##  5 Louisville Race… Loui… KY    2011-01-10 00:00:00        3          1        1
##  6 Louisville Race… Loui… KY    2011-01-10 00:00:00        3          2        1
##  7 Louisville Race… Loui… KY    2011-01-10 00:00:00        3          3        1
##  8 Louisville Race… Loui… KY    2011-01-10 00:00:00        3          4        1
##  9 Louisville Race… Loui… KY    2011-01-10 00:00:00        3          5        1
## 10 Ann Arbor Speed  Ann … MI    2011-01-11 00:00:00        4          1        1
## # ℹ 15,634 more rows
## # ℹ 6 more variables: price <dbl>, total_price <dbl>, model <chr>,
## #   category_1 <chr>, category_2 <chr>, frame_material <chr>
# pull
bike_orderlines_tbl %>%
  pull(total_price) %>%
  mean()
## [1] 4540.548
#select_if
bike_orderlines_tbl %>%
  select_if(is.numeric)
## # A tibble: 15,644 × 5
##    order_id order_line quantity price total_price
##       <dbl>      <dbl>    <dbl> <dbl>       <dbl>
##  1        1          1        1  6070        6070
##  2        1          2        1  5970        5970
##  3        2          1        1  2770        2770
##  4        2          2        1  5970        5970
##  5        3          1        1 10660       10660
##  6        3          2        1  3200        3200
##  7        3          3        1 12790       12790
##  8        3          4        1  5330        5330
##  9        3          5        1  1570        1570
## 10        4          1        1  4800        4800
## # ℹ 15,634 more rows
bikes_tbl %>%
  select(model,price)
## # A tibble: 97 × 2
##    model                          price
##    <chr>                          <dbl>
##  1 Supersix Evo Black Inc.        12790
##  2 Supersix Evo Hi-Mod Team       10660
##  3 Supersix Evo Hi-Mod Dura Ace 1  7990
##  4 Supersix Evo Hi-Mod Dura Ace 2  5330
##  5 Supersix Evo Hi-Mod Utegra      4260
##  6 Supersix Evo Red                3940
##  7 Supersix Evo Ultegra 3          3200
##  8 Supersix Evo Ultegra 4          2660
##  9 Supersix Evo 105                2240
## 10 Supersix Evo Tiagra             1840
## # ℹ 87 more rows
#filter()
bikes_tbl %>%
  select(model,price) %>%
  filter(price >mean(price))
## # A tibble: 35 × 2
##    model                          price
##    <chr>                          <dbl>
##  1 Supersix Evo Black Inc.        12790
##  2 Supersix Evo Hi-Mod Team       10660
##  3 Supersix Evo Hi-Mod Dura Ace 1  7990
##  4 Supersix Evo Hi-Mod Dura Ace 2  5330
##  5 Supersix Evo Hi-Mod Utegra      4260
##  6 CAAD12 Black Inc                5860
##  7 CAAD12 Disc Dura Ace            4260
##  8 Synapse Hi-Mod Disc Black Inc.  9590
##  9 Synapse Hi-Mod Disc Red         7460
## 10 Synapse Hi-Mod Dura Ace         5860
## # ℹ 25 more rows
bikes_tbl %>%
  select(model,price) %>%
  filter((price > 5000) | (price < 1000)) %>%
  arrange(desc(price)) %>%
  view()

bikes_tbl %>%
  select(model,price) %>%
  filter((price > 6000),
          model %>% str_detect("Supersix")) %>%
  view()

bike_orderlines_tbl %>% filter(category_2 == “Over Mountain”) %>%

# slice(): filtering with row numbers
bikes_tbl %>%
  arrange(price) %>%
  slice(1:5) %>%
  view()


# distinct(): unique values
bike_orderlines_tbl %>%
  distinct(category_1, category_2) %>%
  view()

# mutate(): To add columns to our data
bike_orderlines_tbl %>%
  select(order_date, model, quantity, price) %>%
  mutate(total_price = quantity * price)
## # A tibble: 15,644 × 5
##    order_date          model                          quantity price total_price
##    <dttm>              <chr>                             <dbl> <dbl>       <dbl>
##  1 2011-01-07 00:00:00 Jekyll Carbon 2                       1  6070        6070
##  2 2011-01-07 00:00:00 Trigger Carbon 2                      1  5970        5970
##  3 2011-01-10 00:00:00 Beast of the East 1                   1  2770        2770
##  4 2011-01-10 00:00:00 Trigger Carbon 2                      1  5970        5970
##  5 2011-01-10 00:00:00 Supersix Evo Hi-Mod Team              1 10660       10660
##  6 2011-01-10 00:00:00 Jekyll Carbon 4                       1  3200        3200
##  7 2011-01-10 00:00:00 Supersix Evo Black Inc.               1 12790       12790
##  8 2011-01-10 00:00:00 Supersix Evo Hi-Mod Dura Ace 2        1  5330        5330
##  9 2011-01-10 00:00:00 Synapse Disc 105                      1  1570        1570
## 10 2011-01-11 00:00:00 Synapse Carbon Disc Ultegra D…        1  4800        4800
## # ℹ 15,634 more rows

Binning with ntile()

bike_orderlines_tbl %>% mutate(total_price_binned = ntile(total_price, 3)) %>% view()

case_when

bike_orderlines_tbl %?% mutate(total_price_binned = ntile(total_price, 3)) %>% mutate(total_price_binned2 = case_when( total_price > quantile(total_price 0.75) ~ “High” ))

# Joining Data by Keys: left_join()
left_join
## function (x, y, by = NULL, copy = FALSE, suffix = c(".x", ".y"), 
##     ..., keep = NULL) 
## {
##     UseMethod("left_join")
## }
## <bytecode: 0x55ecf486a658>
## <environment: namespace:dplyr>
left_join(orderline_tbl, bikes_tbl,
          by = c("product.id" ="bike.id"))
## # A tibble: 15,644 × 10
##    ...1  order.id order.line order.date          customer.id product.id quantity
##    <chr>    <dbl>      <dbl> <dttm>                    <dbl>      <dbl>    <dbl>
##  1 1            1          1 2011-01-07 00:00:00           2         48        1
##  2 2            1          2 2011-01-07 00:00:00           2         52        1
##  3 3            2          1 2011-01-10 00:00:00          10         76        1
##  4 4            2          2 2011-01-10 00:00:00          10         52        1
##  5 5            3          1 2011-01-10 00:00:00           6          2        1
##  6 6            3          2 2011-01-10 00:00:00           6         50        1
##  7 7            3          3 2011-01-10 00:00:00           6          1        1
##  8 8            3          4 2011-01-10 00:00:00           6          4        1
##  9 9            3          5 2011-01-10 00:00:00           6         34        1
## 10 10           4          1 2011-01-11 00:00:00          22         26        1
## # ℹ 15,634 more rows
## # ℹ 3 more variables: model <chr>, description <chr>, price <dbl>