Skip to content

Instantly share code, notes, and snippets.

@johnmackintosh
Last active January 17, 2021 00:04
Show Gist options
  • Save johnmackintosh/972afb23997c2f982dca182aae88e1d1 to your computer and use it in GitHub Desktop.
Save johnmackintosh/972afb23997c2f982dca182aae88e1d1 to your computer and use it in GitHub Desktop.
library(here)
library(data.table)
library(stringr)
setwd(here("2021-02"))
# read in and process
DT <- fread('Input.csv')
DT[,`:=`(`Order Date` = as.IDate(DT$`Order Date`, format = "%d/%m/%Y"),
`Shipping Date` = as.IDate(DT$`Shipping Date`, format = "%d/%m/%Y"))]
DT[, Model := str_replace_all(DT$Model, "[^:A-Za-z:]", "")]
DT[, Order_Value := `Value per Bike` * Quantity]
DT[, days_to_ship := `Shipping Date` - `Order Date`,]
# create tables 1 and 2
output1 <- DT[,.SD,.SDcols = c('Model','Bike Type', 'Quantity',
'Order_Value', 'Value per Bike')]
output1[, `:=`(Quantity = sum(Quantity),
Order_Value = sum(Order_Value),
Avg_Value = mean(`Value per Bike`,na.rm = TRUE)),
by = .(Model, `Bike Type`)]
output1[, Avg_Value := round(Avg_Value,1)]
output1[, `Value per Bike` := NULL]
output1 <- unique(output1)
setnames(output1, old = 'Model', new = 'Brand')
## output 2
output2 <- DT[,.SD,.SDcols = c('Model','Store', 'Quantity',
'Order_Value', 'days_to_ship')]
output2[, `:=`(Quantity = sum(Quantity),
Order_Value = sum(Order_Value),
Avg_Days_to_Ship = mean(days_to_ship,na.rm = TRUE)),
by = .(Model, Store)]
output2[, Avg_Days_to_Ship := round(Avg_Days_to_Ship,1)]
output2$days_to_ship <- NULL
output2 <- unique(output2)
setnames(output2, old = 'Model', new = 'Brand')
# checks
dim(output1)
str(output1)
dim(output2)
str(output2)
output1 <- fwrite(output1,"2021-02-output1.tsv", sep = "\t")
output2 <- fwrite(output2,"2021-02-output2.tsv", sep = "\t")
Brand Bike Type Quantity Order_Value Avg_Value
GIA Mountain 425 1021329 2378.9
GIA Gravel 323 733087 2303.2
GIA Road 407 896695 2184.7
SPEC Gravel 974 2295397 2355.6
SPEC Mountain 960 2344504 2422.7
SPEC Road 937 2195597 2356.3
ORRO Mountain 87 206550 2382.4
ORRO Road 84 181300 2237.7
ORRO Gravel 151 411644 2640
BROM Gravel 186 433885 2335.5
BROM Mountain 277 674770 2359.3
BROM Road 257 656539 2500.7
KONA Mountain 330 820537 2487
KONA Road 273 647684 2430.2
KONA Gravel 324 791841 2463.7
Brand Store Quantity Order_Value Avg_Days_to_Ship
GIA Manchester 204 466613 11
GIA Birmingham 269 581733 9.9
GIA York 251 593793 10.4
GIA Leeds 203 460151 11.2
GIA London 228 548821 10.7
SPEC Leeds 570 1431894 10.4
SPEC London 578 1358343 11.2
SPEC York 458 1105777 10.4
SPEC Birmingham 651 1488013 10.6
SPEC Manchester 614 1451471 11.1
ORRO Leeds 55 126334 11.9
ORRO Birmingham 86 216169 11.4
ORRO Manchester 43 118800 8.4
ORRO London 61 151734 9.4
ORRO York 77 186457 9
BROM London 133 324635 11
BROM Manchester 137 339832 10.9
BROM Leeds 150 389116 9.8
BROM York 145 361852 9.8
BROM Birmingham 155 349759 11.8
KONA London 216 524879 10.7
KONA Manchester 148 370481 10.5
KONA York 171 430667 11.1
KONA Birmingham 184 435694 10.6
KONA Leeds 208 498341 9.2
@johnmackintosh
Copy link
Author

image

@johnmackintosh
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment