Load Libraries and Data

library(tidyverse)
library(tidyquant)
library(lubridate)
library(readxl)

# Load data - adjust path if needed
# Option 1: If RDS file exists
if (file.exists('bike_orderlines.rds')) {
  bike_orderlines_wrangled_tbl <- readRDS('bike_orderlines.rds')
} else {
  # Option 2: Create from Excel files
  bikes_tbl <- read_excel("bikes.xlsx")
  bikeshops_tbl <- read_excel("bikeshops.xlsx")
  orderlines_tbl <- read_excel("orderlines.xlsx")
  
  bike_orderlines_wrangled_tbl <- left_join(orderlines_tbl, bikes_tbl, 
                                             by = c("product.id" = "bike.id")) %>% 
    left_join(bikeshops_tbl, by = c("customer.id" = "bikeshop.id")) %>%
    separate(description, 
             into = c('category.1', 'category.2', 'frame.material'), 
             sep  = ' - ') %>% 
    separate(location, 
             into = c('city', 'state'), 
             sep  = ', ',
             remove = FALSE) %>% 
    mutate(total.price = price * quantity) %>% 
    select(-...1, -location) %>% 
    select(contains('date'), contains('id'), 
           contains('order'), 
           quantity, price, total.price, 
           everything()) %>% 
    rename(order_date = order.date) %>%
    set_names(names(.) %>% str_replace_all("\\.", "_"))
}