library(readr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ purrr     1.0.2
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ── 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(dplyr)
#install.packages("forcats")
library(forcats)
Iowa_Liquor_Small <- read_csv("U:/_MT Student File Area/tberegovska/DATA222/DataRepo/IowaLiquorStores/Iowa_Liquor_Small.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 4162976 Columns: 24
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (13): Invoice/Item Number, Store Name, Address, City, Store Location, C...
## dbl  (10): Store Number, Zip Code, Category, Vendor Number, Item Number, Pac...
## date  (1): 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.
Iowa_Liquor_Small_Working <- Iowa_Liquor_Small 
Iowa_less_var <- Iowa_Liquor_Small_Working %>%
  select(`Invoice/Item Number`, `Store Number`, `Zip Code`, County, Category, `Vendor Number`, `Item Number`, Pack, `Bottle Volume (ml)`, `State Bottle Cost`, `State Bottle Retail`, `Bottles Sold`, `Sale (Dollars)`, `Volume Sold (Gallons)`)
head(Iowa_less_var)  # I only wanted these specific values so I used select
## # A tibble: 6 × 14
##   `Invoice/Item Number` `Store Number` `Zip Code` County     Category
##   <chr>                          <dbl>      <dbl> <chr>         <dbl>
## 1 S30383800018                    3621      51201 O'Brien     1012200
## 2 S30165000183                    2552      52404 Linn        1701100
## 3 S30172700001                    2513      52240 Johnson     1701100
## 4 S30188900079                    3869      52241 Johnson     1701100
## 5 S30193900075                    2106      50613 Black Hawk  1701100
## 6 S30441600009                    2248      50312 Polk        1701100
## # ℹ 9 more variables: `Vendor Number` <dbl>, `Item Number` <dbl>, Pack <dbl>,
## #   `Bottle Volume (ml)` <dbl>, `State Bottle Cost` <chr>,
## #   `State Bottle Retail` <chr>, `Bottles Sold` <dbl>, `Sale (Dollars)` <chr>,
## #   `Volume Sold (Gallons)` <dbl>
Iowa_less_var %>% mutate_at(vars(`State Bottle Cost`,`Zip Code`, `Bottles Sold`, `State Bottle Retail`, `Sale (Dollars)`, `Volume Sold (Gallons)`), as.numeric)
## Warning: There were 3 warnings in `mutate()`.
## The first warning was:
## ℹ In argument: `State Bottle Cost = .Primitive("as.double")(`State Bottle
##   Cost`)`.
## Caused by warning:
## ! NAs introduced by coercion
## ℹ Run `dplyr::last_dplyr_warnings()` to see the 2 remaining warnings.
## # A tibble: 4,162,976 × 14
##    `Invoice/Item Number` `Store Number` `Zip Code` County     Category
##    <chr>                          <dbl>      <dbl> <chr>         <dbl>
##  1 S30383800018                    3621      51201 O'Brien     1012200
##  2 S30165000183                    2552      52404 Linn        1701100
##  3 S30172700001                    2513      52240 Johnson     1701100
##  4 S30188900079                    3869      52241 Johnson     1701100
##  5 S30193900075                    2106      50613 Black Hawk  1701100
##  6 S30441600009                    2248      50312 Polk        1701100
##  7 S30032900145                    2651      50677 Bremer      1701100
##  8 S30039900002                    2285      52240 Johnson     1701100
##  9 S30053000037                    2603      52722 Scott       1701100
## 10 S30000200003                    2643      50701 Black Hawk  1701100
## # ℹ 4,162,966 more rows
## # ℹ 9 more variables: `Vendor Number` <dbl>, `Item Number` <dbl>, Pack <dbl>,
## #   `Bottle Volume (ml)` <dbl>, `State Bottle Cost` <dbl>,
## #   `State Bottle Retail` <dbl>, `Bottles Sold` <dbl>, `Sale (Dollars)` <dbl>,
## #   `Volume Sold (Gallons)` <dbl>
Iowa_less_var %>% mutate_at(vars(`Invoice/Item Number`, Category, County, Pack), as.factor)
## # A tibble: 4,162,976 × 14
##    `Invoice/Item Number` `Store Number` `Zip Code` County     Category
##    <fct>                          <dbl>      <dbl> <fct>      <fct>   
##  1 S30383800018                    3621      51201 O'Brien    1012200 
##  2 S30165000183                    2552      52404 Linn       1701100 
##  3 S30172700001                    2513      52240 Johnson    1701100 
##  4 S30188900079                    3869      52241 Johnson    1701100 
##  5 S30193900075                    2106      50613 Black Hawk 1701100 
##  6 S30441600009                    2248      50312 Polk       1701100 
##  7 S30032900145                    2651      50677 Bremer     1701100 
##  8 S30039900002                    2285      52240 Johnson    1701100 
##  9 S30053000037                    2603      52722 Scott      1701100 
## 10 S30000200003                    2643      50701 Black Hawk 1701100 
## # ℹ 4,162,966 more rows
## # ℹ 9 more variables: `Vendor Number` <dbl>, `Item Number` <dbl>, Pack <fct>,
## #   `Bottle Volume (ml)` <dbl>, `State Bottle Cost` <chr>,
## #   `State Bottle Retail` <chr>, `Bottles Sold` <dbl>, `Sale (Dollars)` <chr>,
## #   `Volume Sold (Gallons)` <dbl>
set.seed(1)
random_sample <- Iowa_less_var[sample(nrow(Iowa_less_var), 20000),]
random_sample_less <- random_sample %>%
  select(County , Category,Pack, `Bottles Sold`, `Sale (Dollars)`, `Volume Sold (Gallons)`, -`Invoice/Item Number`, -`Store Number`, -`Zip Code`, -`Vendor Number`, -`Item Number`, -`Bottle Volume (ml)`, -`State Bottle Cost`, -`State Bottle Retail`)
head(random_sample_less)  # I only wanted these specific values so I used select
## # A tibble: 6 × 6
##   County   Category  Pack `Bottles Sold` `Sale (Dollars)` Volume Sold (Gallons…¹
##   <chr>       <dbl> <dbl>          <dbl> <chr>                             <dbl>
## 1 POLK      1082000     6              1 $88.50                             0.19
## 2 POLK      1031200    12              3 $9.75                              0.59
## 3 Fayette   1062500    12              1 $188.88                            0.26
## 4 POLK      1081000     6              1 $43.56                             0.13
## 5 Black H…  1031080    12             12 $124.20                            2.38
## 6 SCOTT     1082000    12              2 $15.74                             0.39
## # ℹ abbreviated name: ¹​`Volume Sold (Gallons)`
random_sample_less %>%
  count(Category, name = "freq") %>%
  group_by(freq) %>%
  arrange((freq)) %>%
  slice_max(freq, n=10) %>%
  ungroup()
## # A tibble: 93 × 2
##    Category  freq
##       <dbl> <int>
##  1  1081015     1
##  2  1082010     1
##  3  1011800     2
##  4  1081210     2
##  5  1081350     2
##  6  1091400     2
##  7  1022300     3
##  8  1031090     3
##  9  1041150     3
## 10  1081220     3
## # ℹ 83 more rows
random_sample_less
## # A tibble: 20,000 × 6
##    County  Category  Pack `Bottles Sold` `Sale (Dollars)` Volume Sold (Gallons…¹
##    <chr>      <dbl> <dbl>          <dbl> <chr>                             <dbl>
##  1 POLK     1082000     6              1 $88.50                             0.19
##  2 POLK     1031200    12              3 $9.75                              0.59
##  3 Fayette  1062500    12              1 $188.88                            0.26
##  4 POLK     1081000     6              1 $43.56                             0.13
##  5 Black …  1031080    12             12 $124.20                            2.38
##  6 SCOTT    1082000    12              2 $15.74                             0.39
##  7 LINN     1011200    12              1 $188.88                            0.19
##  8 Scott    1011200     6              2 $57.52                             0.4 
##  9 DUBUQUE  1031100    12              1 $148.56                            0.19
## 10 Black …  1081010    12              5 $30.40                             1.32
## # ℹ 19,990 more rows
## # ℹ abbreviated name: ¹​`Volume Sold (Gallons)`
renamed_categories <- random_sample_less %>%
  mutate(category = recode(Category, 
             `1012100` = "Canadian Whiskies",
             `1031100` = "100 Proof Vodka",
             `1011200` = "Straight Bourbon Whiskies",
             `1031200` = "Straight Bourbon Whiskies",
             `1031080` = "Vodka 80 Proof",
             `1081600` = "Whiskey Liqueur",
             `1011100` = "Blended Whiskies",
             `1022100` = "Tequila",
             `1062200` = "Puerto Rico & Virgin Islands Rum",
             `1081400` = "Triple Sec"
                                   ))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `category = recode(...)`.
## Caused by warning:
## ! Unreplaced values treated as NA as `.x` is not compatible.
## Please specify replacements exhaustively or supply `.default`.
renamed_categories
## # A tibble: 20,000 × 7
##    County  Category  Pack `Bottles Sold` `Sale (Dollars)` Volume Sold (Gallons…¹
##    <chr>      <dbl> <dbl>          <dbl> <chr>                             <dbl>
##  1 POLK     1082000     6              1 $88.50                             0.19
##  2 POLK     1031200    12              3 $9.75                              0.59
##  3 Fayette  1062500    12              1 $188.88                            0.26
##  4 POLK     1081000     6              1 $43.56                             0.13
##  5 Black …  1031080    12             12 $124.20                            2.38
##  6 SCOTT    1082000    12              2 $15.74                             0.39
##  7 LINN     1011200    12              1 $188.88                            0.19
##  8 Scott    1011200     6              2 $57.52                             0.4 
##  9 DUBUQUE  1031100    12              1 $148.56                            0.19
## 10 Black …  1081010    12              5 $30.40                             1.32
## # ℹ 19,990 more rows
## # ℹ abbreviated name: ¹​`Volume Sold (Gallons)`
## # ℹ 1 more variable: category <chr>
Cat_by_count <- renamed_categories %>%
  group_by(category) %>%
  summarise(count = n()) %>%
  ungroup()
head(Cat_by_count)
## # A tibble: 6 × 2
##   category                         count
##   <chr>                            <int>
## 1 100 Proof Vodka                   1193
## 2 Blended Whiskies                   861
## 3 Canadian Whiskies                 1919
## 4 Puerto Rico & Virgin Islands Rum   629
## 5 Straight Bourbon Whiskies         2271
## 6 Tequila                            651
Cat_by_count <- renamed_categories %>%
  select(County, category) %>%
  group_by(category) %>%
  arrange(category) %>%
  na.omit() %>%
  ungroup()
unique(Cat_by_count$category)
## [1] "100 Proof Vodka"                  "Blended Whiskies"                
## [3] "Canadian Whiskies"                "Puerto Rico & Virgin Islands Rum"
## [5] "Straight Bourbon Whiskies"        "Tequila"                         
## [7] "Triple Sec"                       "Vodka 80 Proof"                  
## [9] "Whiskey Liqueur"
Cat_most <- Cat_by_count %>%
  filter(!is.na(category)) %>% slice(1:10)
Cat_most
## # A tibble: 10 × 2
##    County     category       
##    <chr>      <chr>          
##  1 DUBUQUE    100 Proof Vodka
##  2 POLK       100 Proof Vodka
##  3 BUENA VIST 100 Proof Vodka
##  4 DUBUQUE    100 Proof Vodka
##  5 BREMER     100 Proof Vodka
##  6 BLACK HAWK 100 Proof Vodka
##  7 CEDAR      100 Proof Vodka
##  8 BLACK HAWK 100 Proof Vodka
##  9 BLACK HAWK 100 Proof Vodka
## 10 BLACK HAWK 100 Proof Vodka
Cat_least <- Cat_by_count %>%
  filter(!is.na(category)) %>% tail(n = 10) %>% arrange(category)
Cat_least
## # A tibble: 10 × 2
##    County        category       
##    <chr>         <chr>          
##  1 HARRISON      Whiskey Liqueur
##  2 Sioux         Whiskey Liqueur
##  3 Story         Whiskey Liqueur
##  4 POLK          Whiskey Liqueur
##  5 MARION        Whiskey Liqueur
##  6 Polk          Whiskey Liqueur
##  7 POTTAWATTA    Whiskey Liqueur
##  8 JOHNSON       Whiskey Liqueur
##  9 Pottawattamie Whiskey Liqueur
## 10 LEE           Whiskey Liqueur
Cat_by_count_1 <- Cat_by_count %>%
 filter(if_any(everything(), ~ .x=="100 Proof Vodka"))
top_categories <- Cat_by_count %>%
  count(category, name = "freq") %>%
  group_by(freq) %>%
  arrange((freq))
ggplot(data = top_categories, mapping = aes( x=category, y=freq)) +
  geom_col() +
  scale_x_discrete(guide = guide_axis(n.dodge=2)) +
  scale_y_discrete(guide = guide_axis(n.dodge=2))