Skip to content

Instantly share code, notes, and snippets.

@jrosell
Created September 18, 2025 07:42
Show Gist options
  • Select an option

  • Save jrosell/ec5f0c371635b295febe077520465eb1 to your computer and use it in GitHub Desktop.

Select an option

Save jrosell/ec5f0c371635b295febe077520465eb1 to your computer and use it in GitHub Desktop.
Example of use case for the dplyr::replace_values function.
# Cocktails example by Hadley in 2021: Video https://youtu.be/kHFmtKCI_F4?si=HC4S3B-RI_l96sU4&t=1810 | Code: https://gist.github.com/hadley/a892ff8f00973e3bc864851deae8315f
# Added: New version using the replace_values function.

# Installing the new branch with the new feature
pak::pak("tidyverse/dplyr@feature/case-family")
#> ℹ Loading metadata database
#> ✔ Loading metadata database ... done
#> 
#> 
#> 
#> ℹ No downloads are needed
#> ✔ 1 pkg + 14 deps: kept 13 [4.9s]

library(tidyverse)

cocktails <- readr::read_csv(
  'https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2020/2020-05-26/boston_cocktails.csv'
)
#> Rows: 3643 Columns: 6
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr (4): name, category, ingredient, measure
#> dbl (2): row_id, ingredient_number
#> 
#> ℹ 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.

# Are name and row_id equivalent? -----------------------------------------

cocktails %>%
  group_by(name) %>%
  summarise(ids = n_distinct(row_id)) %>%
  filter(ids > 1)
#> # A tibble: 0 × 2
#> # ℹ 2 variables: name <chr>, ids <int>
# Yes, they are

# How many ingredients are in each cocktail? ------------------------------
cocktails %>%
  count(name) %>%
  count(n)
#> Storing counts in `nn`, as `n` already present in input
#> ℹ Use `name = "new_name"` to pick a new name.
#> # A tibble: 6 × 2
#>       n    nn
#>   <int> <int>
#> 1     1    31
#> 2     2   118
#> 3     3   292
#> 4     4   297
#> 5     5   194
#> 6     6    57

# These 1-ingredient cocktails suggest that non-alcoholic ingredients
# are not included
cocktails %>%
  group_by(name) %>%
  filter(n() == 1)
#> # A tibble: 31 × 6
#> # Groups:   name [31]
#>    name                  category    row_id ingredient_number ingredient measure
#>    <chr>                 <chr>        <dbl>             <dbl> <chr>      <chr>  
#>  1 Brandy and Soda       Cocktail C…    149                 1 Brandy     2 oz   
#>  2 Vodka and Tonic       Vodka          156                 1 Vodka      2 oz   
#>  3 Vodka and Apple Juice Vodka          162                 1 Vodka      2 oz   
#>  4 Limestone             Cocktail C…    164                 1 Bourbon    1 1/2 …
#>  5 Bourbon On The Rocks  Cocktail C…    170                 1 Bourbon    2 oz   
#>  6 Saronno Mist          Cocktail C…    183                 1 Amaretto … 1 1/2 …
#>  7 Mandarine Mist        Cocktail C…    195                 1 Mandarine… 1 1/2 …
#>  8 Hill Billy Highball   Cocktail C…    216                 1 Georgia M… 2 oz   
#>  9 Cafe di Saronno       Cocktail C…    274                 1 Amaretto … 1 oz   
#> 10 Bourbon Highball      Whiskies       296                 1 Bourbon w… 2 oz   
#> # ℹ 21 more rows

cocktails %>%
  group_by(name) %>%
  filter(n() == 2)
#> # A tibble: 236 × 6
#> # Groups:   name [118]
#>    name                  category    row_id ingredient_number ingredient measure
#>    <chr>                 <chr>        <dbl>             <dbl> <chr>      <chr>  
#>  1 Apple Pie             Cordials a…      3                 1 Apple sch… 3 oz   
#>  2 Apple Pie             Cordials a…      3                 2 Cinnamon … 1 oz   
#>  3 Caribbean Champagne   Cocktail C…      9                 1 Light Rum  1/2 oz 
#>  4 Caribbean Champagne   Cocktail C…      9                 2 Creme de … 1/2 oz 
#>  5 Amaretto Stinger      Cordials a…     17                 1 Amaretto   1 1/2 …
#>  6 Amaretto Stinger      Cordials a…     17                 2 White cre… 3/4 oz 
#>  7 Amaretto And Cream    Cordials a…     44                 1 Amaretto   1 1/2 …
#>  8 Amaretto And Cream    Cordials a…     44                 2 half-and-… 1 1/2 …
#>  9 Montreal Club Bouncer Cocktail C…     50                 1 Gin        1 1/2 …
#> 10 Montreal Club Bouncer Cocktail C…     50                 2 Anisette   1/2 oz 
#> # ℹ 226 more rows

# How big is each cocktail? -----------------------------------------------

cocktails %>% count(measure, sort = TRUE)
#> # A tibble: 42 × 2
#>    measure       n
#>    <chr>     <int>
#>  1 1 oz       1102
#>  2 1/2 oz      776
#>  3 1 1/2 oz    494
#>  4 2 oz        426
#>  5 3/4 oz      416
#>  6 1/4 oz      214
#>  7 3 oz         67
#>  8 4 oz         23
#>  9 1 1/4 oz     21
#> 10 For glass    19
#> # ℹ 32 more rows

non_spirit <- c(
  "Chilled Champagne",
  "Water",
  "Orange Juice",
  "Cranberry Juice",
  "Light Cream (if desired)",
  "Fresh orange juice",
  "Orange juice"
)

# Have removed all cocktails with ounces of bitters - that is most
# likely a data entry error (it certainly doesn't give me much confidence
# in the quality of this data)

sizes <- cocktails %>%
  filter(str_detect(measure, "oz")) %>%
  filter(!str_detect(ingredient, fixed("bitters", ignore_case = TRUE))) %>%
  filter(!ingredient %in% non_spirit) %>%
  mutate(oz = str_replace(measure, " oz", "")) %>%
  mutate(oz = str_replace(oz, " ?1/2", ".5")) %>%
  mutate(oz = str_replace(oz, " ?1/4", ".25")) %>%
  mutate(oz = str_replace(oz, " ? ?3/4", ".75")) %>%
  mutate(oz = str_replace(oz, " ?1/3", ".33")) %>%
  mutate(oz = str_replace(oz, " ?2/3", ".33")) %>%
  mutate(oz = as.numeric(oz))

filter(sizes, oz > 3)
#> # A tibble: 28 × 7
#>    name             category   row_id ingredient_number ingredient measure    oz
#>    <chr>            <chr>       <dbl>             <dbl> <chr>      <chr>   <dbl>
#>  1 Jocose Julep     Whiskies       21                 5 Fresh min… 5 oz        5
#>  2 Incider Cocktail Whiskies       62                 2 Apple cid… 4 oz        4
#>  3 Greyhound        Cocktail …     74                 2 Grapefrui… 5 oz        5
#>  4 Buddy's Favorite Cocktail …    169                 2 Cold Water 6 oz        6
#>  5 Tea Saronno      Cocktail …    229                 1 Hot Tea    6 oz        6
#>  6 Salty Dog        Vodka         257                 3 Grapefrui… 5 oz        5
#>  7 Redhead Martini  Vodka         277                 1 Strawberr… 4 oz        4
#>  8 Bloody Scotsman  Whiskies      304                 1 Smoky Sco… 24 oz      24
#>  9 Bloody Scotsman  Whiskies      304                 2 Tomato Ju… 24 oz      24
#> 10 Bloody Scotsman  Whiskies      304                 3 V8 cockta… 24 oz      24
#> # ℹ 18 more rows
filter(sizes, oz > 10)
#> # A tibble: 3 × 7
#>   name            category row_id ingredient_number ingredient     measure    oz
#>   <chr>           <chr>     <dbl>             <dbl> <chr>          <chr>   <dbl>
#> 1 Bloody Scotsman Whiskies    304                 1 Smoky Scotch … 24 oz      24
#> 2 Bloody Scotsman Whiskies    304                 2 Tomato Juice   24 oz      24
#> 3 Bloody Scotsman Whiskies    304                 3 V8 cocktail j… 24 oz      24

total_size <- sizes %>% group_by(name) %>% summarise(n = n(), oz = sum(oz))

total_size %>% filter(oz > 20)
#> # A tibble: 1 × 3
#>   name                n    oz
#>   <chr>           <int> <dbl>
#> 1 Bloody Scotsman     5    78

total_size %>%
  filter(oz < 20) %>%
  ggplot(aes(oz)) +
  geom_histogram(binwidth = 0.5)

total_size %>%
  filter(oz > 6) %>%
  semi_join(cocktails, ., by = "name") %>%
  print(n = Inf)
#> # A tibble: 258 × 6
#>     name                    category row_id ingredient_number ingredient measure
#>     <chr>                   <chr>     <dbl>             <dbl> <chr>      <chr>  
#>   1 Casa Blanca             Cocktai…      8                 1 Light Rum  2 oz   
#>   2 Casa Blanca             Cocktai…      8                 2 Lime Juice 1 1/2 …
#>   3 Casa Blanca             Cocktai…      8                 3 Triple Sec 1 1/2 …
#>   4 Casa Blanca             Cocktai…      8                 4 Maraschino 1 1/2 …
#>   5 Jocose Julep            Whiskies     21                 1 Bourbon w… 2 1/2 …
#>   6 Jocose Julep            Whiskies     21                 2 Green cre… 1/2 oz 
#>   7 Jocose Julep            Whiskies     21                 3 Fresh Lim… 1 oz   
#>   8 Jocose Julep            Whiskies     21                 4 Simple Sy… 1/2 oz 
#>   9 Jocose Julep            Whiskies     21                 5 Fresh min… 5 oz   
#>  10 Pollyanna Cocktai       Cocktai…     45                 1 Orange     3 oz   
#>  11 Pollyanna Cocktai       Cocktai…     45                 2 Pineapple  3 oz   
#>  12 Pollyanna Cocktai       Cocktai…     45                 3 Gin        2 oz   
#>  13 Pollyanna Cocktai       Cocktai…     45                 4 Sweet Ver… 1/2 oz 
#>  14 Pollyanna Cocktai       Cocktai…     45                 5 Grenadine  1/2 oz 
#>  15 Incider Cocktail        Whiskies     62                 1 Rye or bo… 1 1/2 …
#>  16 Incider Cocktail        Whiskies     62                 2 Apple cid… 4 oz   
#>  17 Incider Cocktail        Whiskies     62                 3 Apple sli… 1 oz   
#>  18 Greyhound               Cocktai…     74                 1 Gin        1 1/2 …
#>  19 Greyhound               Cocktai…     74                 2 Grapefrui… 5 oz   
#>  20 Buddy's Favorite        Cocktai…    169                 1 Bourbon    1 1/2 …
#>  21 Buddy's Favorite        Cocktai…    169                 2 Cold Water 6 oz   
#>  22 Bourbon Cobbler         Cocktai…    171                 1 Bourbon    2 1/2 …
#>  23 Bourbon Cobbler         Cocktai…    171                 2 Lemon Jui… 1 oz   
#>  24 Bourbon Cobbler         Cocktai…    171                 3 Grapefrui… 2 oz   
#>  25 Bourbon Cobbler         Cocktai…    171                 4 Almond ex… 1 1/2 …
#>  26 Saronno Colada (Nutty … Cocktai…    182                 1 Amaretto … 3 oz   
#>  27 Saronno Colada (Nutty … Cocktai…    182                 2 Coconut M… 3 oz   
#>  28 Saronno Colada (Nutty … Cocktai…    182                 3 Crushed P… 3 oz   
#>  29 Tiger Tanaka            Vodka       218                 1 Fresh cil… 3 oz   
#>  30 Tiger Tanaka            Vodka       218                 2 Slice pee… 1 oz   
#>  31 Tiger Tanaka            Vodka       218                 3 Citrus-fl… 2 oz   
#>  32 Tiger Tanaka            Vodka       218                 4 Limoncello 1/2 oz 
#>  33 Tiger Tanaka            Vodka       218                 5 Pineapple… 3/4 oz 
#>  34 Thyme Collins           Vodka       220                 1 Citrus-fl… 2 oz   
#>  35 Thyme Collins           Vodka       220                 2 Mint syrup 1 oz   
#>  36 Thyme Collins           Vodka       220                 3 Fresh lem… 1 oz   
#>  37 Thyme Collins           Vodka       220                 4 Thyme spr… 1 oz   
#>  38 Thyme Collins           Vodka       220                 5 Bitter le… 3 oz   
#>  39 Thyme Collins           Vodka       220                 6 Long thym… 2 oz   
#>  40 Tea Saronno             Cocktai…    229                 1 Hot Tea    6 oz   
#>  41 Tea Saronno             Cocktai…    229                 2 Amaretto … 1 1/2 …
#>  42 Tea Saronno             Cocktai…    229                 3 Whipped c… 1 oz   
#>  43 Shalom                  Vodka       251                 1 Vodka      1 1/2 …
#>  44 Shalom                  Vodka       251                 2 Madeira    1 oz   
#>  45 Shalom                  Vodka       251                 3 Fresh ora… 1/2 oz 
#>  46 Shalom                  Vodka       251                 4 Bourbon W… 2 oz   
#>  47 Shalom                  Vodka       251                 5 White cre… 3/4 oz 
#>  48 Shalom                  Vodka       251                 6 Fresh lem… 1/2 oz 
#>  49 Salty Dog               Vodka       257                 1 Lemon wed… For gl…
#>  50 Salty Dog               Vodka       257                 2 Vodka      1 1/2 …
#>  51 Salty Dog               Vodka       257                 3 Grapefrui… 5 oz   
#>  52 Redhead Martini         Vodka       277                 1 Strawberr… 4 oz   
#>  53 Redhead Martini         Vodka       277                 2 Fresh lem… 3/4 oz 
#>  54 Redhead Martini         Vodka       277                 3 Simple Sy… 3/4 oz 
#>  55 Redhead Martini         Vodka       277                 4 Citrus-fl… 1 1/2 …
#>  56 Redhead Martini         Vodka       277                 5 Chilled M… 1 oz   
#>  57 California Lemonade     Whiskies    289                 1 Bourbon w… 2 oz   
#>  58 California Lemonade     Whiskies    289                 2 Fresh lem… 1 oz   
#>  59 California Lemonade     Whiskies    289                 3 Fresh Lim… 1 oz   
#>  60 California Lemonade     Whiskies    289                 4 Simple Sy… 1 oz   
#>  61 California Lemonade     Whiskies    289                 5 Grenadine  1/4 oz 
#>  62 California Lemonade     Whiskies    289                 6 Soda wate… 1 oz   
#>  63 Bloody Scotsman         Whiskies    304                 1 Smoky Sco… 24 oz  
#>  64 Bloody Scotsman         Whiskies    304                 2 Tomato Ju… 24 oz  
#>  65 Bloody Scotsman         Whiskies    304                 3 V8 cockta… 24 oz  
#>  66 Bloody Scotsman         Whiskies    304                 4 ketchup    1c     
#>  67 Bloody Scotsman         Whiskies    304                 5 Fresh Lim… 3 oz   
#>  68 Bloody Scotsman         Whiskies    304                 6 Simple Sy… 3 oz   
#>  69 Cole's Raspberry Cream  Cocktai…    328                 1 Raspberry… 1 1/2 …
#>  70 Cole's Raspberry Cream  Cocktai…    328                 2 Raspberry… 1 1/2 …
#>  71 Cole's Raspberry Cream  Cocktai…    328                 3 White cre… 1 1/2 …
#>  72 Cole's Raspberry Cream  Cocktai…    328                 4 Vodka      1 1/2 …
#>  73 Cole's Raspberry Cream  Cocktai…    328                 5 Heavy cre… 2 oz   
#>  74 Pink Rose Fizz          Cocktai…    391                 1 Juice of … 1/2 oz 
#>  75 Pink Rose Fizz          Cocktai…    391                 2 Powdered … 1 oz   
#>  76 Pink Rose Fizz          Cocktai…    391                 3 Egg White  1 oz   
#>  77 Pink Rose Fizz          Cocktai…    391                 4 Light cre… 2 oz   
#>  78 Pink Rose Fizz          Cocktai…    391                 5 Gin        2 oz   
#>  79 Million-Dollar Cocktail Cocktai…    424                 1 Pineapple… 2 oz   
#>  80 Million-Dollar Cocktail Cocktai…    424                 2 Grenadine  1 oz   
#>  81 Million-Dollar Cocktail Cocktai…    424                 3 Egg White  1 oz   
#>  82 Million-Dollar Cocktail Cocktai…    424                 4 Sweet Ver… 3/4 oz 
#>  83 Million-Dollar Cocktail Cocktai…    424                 5 Gin        1 1/2 …
#>  84 Manila Fizz             Cocktai…    432                 1 Gin        2 oz   
#>  85 Manila Fizz             Cocktai…    432                 2 Whole Egg  1 oz   
#>  86 Manila Fizz             Cocktai…    432                 3 Powdered … 1 oz   
#>  87 Manila Fizz             Cocktai…    432                 4 Root Beer  2 oz   
#>  88 Manila Fizz             Cocktai…    432                 5 Juice of … 1/2 oz 
#>  89 Maracuya Mosquito       Vodka       434                 1 Fresh bas… 4 oz   
#>  90 Maracuya Mosquito       Vodka       434                 2 Vodka      1 1/2 …
#>  91 Maracuya Mosquito       Vodka       434                 3 Green Cha… 1/2 oz 
#>  92 Maracuya Mosquito       Vodka       434                 4 Fresh Lim… 1/2 oz 
#>  93 Maracuya Mosquito       Vodka       434                 5 Simple Sy… 1/2 oz 
#>  94 Maracuya Mosquito       Vodka       434                 6 Passion F… 1 oz   
#>  95 Red Swizzle             Cocktai…    455                 1 Juice of … 1 oz   
#>  96 Red Swizzle             Cocktai…    455                 2 Powdered … 1 oz   
#>  97 Red Swizzle             Cocktai…    455                 3 Carbonate… 2 oz   
#>  98 Red Swizzle             Cocktai…    455                 4 Gin        2 oz   
#>  99 Red Swizzle             Cocktai…    455                 5 Bitters    2 oz   
#> 100 Red Swizzle             Cocktai…    455                 6 Grenadine  1 oz   
#> 101 Katana                  Vodka       476                 1 Cucumber … 3 oz   
#> 102 Katana                  Vodka       476                 2 Vodka      1 1/2 …
#> 103 Katana                  Vodka       476                 3 Sake       1/2 oz 
#> 104 Katana                  Vodka       476                 4 Fresh Lim… 3/4 oz 
#> 105 Katana                  Vodka       476                 5 Simple Sy… 3/4 oz 
#> 106 Katana                  Vodka       476                 6 Cucumber   1 oz   
#> 107 Tequila Cocktail        Cocktai…    500                 1 Tequila    2 oz   
#> 108 Tequila Cocktail        Cocktai…    500                 2 Juice of … 1/2 oz 
#> 109 Tequila Cocktail        Cocktai…    500                 3 Grenadine  4 oz   
#> 110 Tequila Cocktail        Cocktai…    500                 4 Egg White  1 oz   
#> 111 Lady Love Fizz          Cocktai…    516                 1 Powdered … 1 oz   
#> 112 Lady Love Fizz          Cocktai…    516                 2 Juice of … 1/2 oz 
#> 113 Lady Love Fizz          Cocktai…    516                 3 Egg White  1 oz   
#> 114 Lady Love Fizz          Cocktai…    516                 4 Gin        2 oz   
#> 115 Lady Love Fizz          Cocktai…    516                 5 Light cre… 2 oz   
#> 116 Dreamy Dorini Smoking … Vodka       557                 1 Vodka      2 oz   
#> 117 Dreamy Dorini Smoking … Vodka       557                 2 Smoky Sco… 1/2 oz 
#> 118 Dreamy Dorini Smoking … Vodka       557                 3 Pastis     4 oz   
#> 119 Cubeltini               Vodka       566                 1 Cucumber … 3 oz   
#> 120 Cubeltini               Vodka       566                 2 Fresh min… 5      
#> 121 Cubeltini               Vodka       566                 3 Simple Sy… 1 1/2 …
#> 122 Cubeltini               Vodka       566                 4 Vodka      2 oz   
#> 123 Cubeltini               Vodka       566                 5 Fresh Lim… 1 oz   
#> 124 Eye-Opener              Cocktai…    581                 1 Egg Yolk   1 oz   
#> 125 Eye-Opener              Cocktai…    581                 2 Powdered … 1/2 oz 
#> 126 Eye-Opener              Cocktai…    581                 3 Anis       1 oz   
#> 127 Eye-Opener              Cocktai…    581                 4 Triple Sec 1 oz   
#> 128 Eye-Opener              Cocktai…    581                 5 White cre… 1 oz   
#> 129 Eye-Opener              Cocktai…    581                 6 Light Rum  2 oz   
#> 130 Tropica Cocktail        Rum - D…    601                 1 Light Rum  2 oz   
#> 131 Tropica Cocktail        Rum - D…    601                 2 Pineapple… 3 oz   
#> 132 Tropica Cocktail        Rum - D…    601                 3 Fresh gra… 1 oz   
#> 133 Tropica Cocktail        Rum - D…    601                 4 Grenadine  1 oz   
#> 134 Three Miller Cocktail   Rum - D…    609                 1 Fresh che… 3 oz   
#> 135 Three Miller Cocktail   Rum - D…    609                 2 Dark rum   2 oz   
#> 136 Three Miller Cocktail   Rum - D…    609                 3 Fresh Lim… 1 oz   
#> 137 Three Miller Cocktail   Rum - D…    609                 4 Simple Sy… 1 oz   
#> 138 Three Miller Cocktail   Rum - D…    609                 5 Angostura… 1 oz   
#> 139 Applejack Punch         Cocktai…    639                 1 Applejack  1 3/4 …
#> 140 Applejack Punch         Cocktai…    639                 2 Grenadine  4 oz   
#> 141 Applejack Punch         Cocktai…    639                 3 Orange Ju… 16 oz  
#> 142 Applejack Punch         Cocktai…    639                 6 Whole Egg  1 oz   
#> 143 Ringo Starr             Rum - D…    679                 1 Red grapes 3 oz   
#> 144 Ringo Starr             Rum - D…    679                 2 Lemon      1/2 oz 
#> 145 Ringo Starr             Rum - D…    679                 3 Fresh min… 6      
#> 146 Ringo Starr             Rum - D…    679                 4 Sugar cube 1 oz   
#> 147 Ringo Starr             Rum - D…    679                 5 African r… 2 oz   
#> 148 Planter's Punch No. 1   Rum - D…    689                 1 Light Rum  2 1/2 …
#> 149 Planter's Punch No. 1   Rum - D…    689                 2 Fresh Lim… 2 oz   
#> 150 Planter's Punch No. 1   Rum - D…    689                 3 Simple Sy… 1/2 oz 
#> 151 Planter's Punch No. 1   Rum - D…    689                 4 Soda water 2 oz   
#> 152 Planter's Punch No. 1   Rum - D…    689                 5 Grenadine  1 oz   
#> 153 Caribbean Cruise        Vodka       713                 1 Vodka      1 oz   
#> 154 Caribbean Cruise        Vodka       713                 2 Light Rum  1/4 oz 
#> 155 Caribbean Cruise        Vodka       713                 3 Coconut-f… 1/4 oz 
#> 156 Caribbean Cruise        Vodka       713                 4 Grenadine  1 oz   
#> 157 Caribbean Cruise        Vodka       713                 5 Pineapple… 4 oz   
#> 158 Caesar                  Vodka       717                 1 Lemon wed… For gl…
#> 159 Caesar                  Vodka       717                 2 Vodka      1 1/2 …
#> 160 Caesar                  Vodka       717                 3 Tomato-cl… 4 oz   
#> 161 Caesar                  Vodka       717                 4 Prepared … 1/2 oz 
#> 162 Caesar                  Vodka       717                 5 each Worc… 1 oz   
#> 163 Bull Shot               Vodka       718                 1 Vodka      1 1/2 …
#> 164 Bull Shot               Vodka       718                 2 Cold beef… 3 oz   
#> 165 Bull Shot               Vodka       718                 3 Worcester… 1 oz   
#> 166 Bull Shot               Vodka       718                 4 Salt       1 oz   
#> 167 Bull Shot               Vodka       718                 5 Freshly g… 1 oz   
#> 168 Bullfrog                Vodka       719                 1 Vodka      1 1/2 …
#> 169 Bullfrog                Vodka       719                 2 Lemonade   5 oz   
#> 170 Bloody Mary             Vodka       728                 1 Vodka      1 1/2 …
#> 171 Bloody Mary             Vodka       728                 2 Tomato Ju… 3 oz   
#> 172 Bloody Mary             Vodka       728                 3 Fresh Lim… 1/4 oz 
#> 173 Bloody Mary             Vodka       728                 4 Worcester… 4 oz   
#> 174 Bloody Mary             Vodka       728                 5 hot red p… 2 -  3…
#> 175 Victor Collins          Cocktai…    735                 1 Old Mr. B… 1 1/2 …
#> 176 Victor Collins          Cocktai…    735                 2 Unsweeten… 3 oz   
#> 177 Victor Collins          Cocktai…    735                 3 Lemon Jui… 3 oz   
#> 178 Victor Collins          Cocktai…    735                 4 Powdered … 1 oz   
#> 179 Bella Fragolia          Vodka       742                 1 Strawberr… 1 oz   
#> 180 Bella Fragolia          Vodka       742                 2 Fresh bas… 4 oz   
#> 181 Bella Fragolia          Vodka       742                 3 Vodka      2 oz   
#> 182 Bella Fragolia          Vodka       742                 4 Fresh lem… 1 oz   
#> 183 Bella Fragolia          Vodka       742                 5 Simple Sy… 1 oz   
#> 184 Basil 8                 Vodka       744                 1 Fresh bas… 3 oz   
#> 185 Basil 8                 Vodka       744                 2 White gra… 5 oz   
#> 186 Basil 8                 Vodka       744                 3 Vodka      1 1/2 …
#> 187 Basil 8                 Vodka       744                 4 Fresh Lim… 3/4 oz 
#> 188 Basil 8                 Vodka       744                 5 each Simp… 1 oz   
#> 189 Yellow Rose Of Texas    Tequila     750                 1 Blanco te… 2 oz   
#> 190 Yellow Rose Of Texas    Tequila     750                 2 Fresh lem… 3/4 oz 
#> 191 Yellow Rose Of Texas    Tequila     750                 3 Simple Sy… 1/2 oz 
#> 192 Yellow Rose Of Texas    Tequila     750                 4 Yellow Ch… 1/4 oz 
#> 193 Yellow Rose Of Texas    Tequila     750                 5 Rose water 3 oz   
#> 194 Waiting On Summer       Tequila     753                 1 Cucumber   3 oz   
#> 195 Waiting On Summer       Tequila     753                 2 Fresh sag… 3 oz   
#> 196 Waiting On Summer       Tequila     753                 3 Fresh Lim… 1/2 oz 
#> 197 Waiting On Summer       Tequila     753                 4 Blanco te… 1 1/2 …
#> 198 Waiting On Summer       Tequila     753                 5 Simple Sy… 3/4 oz 
#> 199 Waiting On Summer       Tequila     753                 6 Ginger al… 3 oz   
#> 200 Tequila Smash           Tequila     777                 1 Blueberri… 4 oz   
#> 201 Tequila Smash           Tequila     777                 2 Bing cher… 4 oz   
#> 202 Tequila Smash           Tequila     777                 3 Blanco te… 2 oz   
#> 203 Tequila Smash           Tequila     777                 4 Maraschin… 1/2 oz 
#> 204 Tequila Smash           Tequila     777                 5 Fresh Lim… 1/2 oz 
#> 205 Strawberry Margarita    Tequila     792                 1 Lime wedg… For gl…
#> 206 Strawberry Margarita    Tequila     792                 2 Fresh str… 3 oz   
#> 207 Strawberry Margarita    Tequila     792                 3 Strawberr… 3/4 oz 
#> 208 Strawberry Margarita    Tequila     792                 4 Blanco te… 1 1/2 …
#> 209 Strawberry Margarita    Tequila     792                 5 Triple Sec 3/4 oz 
#> 210 Strawberry Margarita    Tequila     792                 6 Fresh Lim… 3/4 oz 
#> 211 Spice Of Life           Tequila     795                 1 Cucumber   3 oz   
#> 212 Spice Of Life           Tequila     795                 2 Fresh min… 12     
#> 213 Spice Of Life           Tequila     795                 3 Simple Sy… 3/4 oz 
#> 214 Spice Of Life           Tequila     795                 4 Jalapeno-… 2 oz   
#> 215 Spice Of Life           Tequila     795                 5 Fresh Lim… 3/4 oz 
#> 216 Shady Lady              Tequila     813                 1 Blanco te… 1 oz   
#> 217 Shady Lady              Tequila     813                 2 Melon liq… 1 oz   
#> 218 Shady Lady              Tequila     813                 3 Fresh gra… 4 oz   
#> 219 Shady Lady              Tequila     813                 4 Fresh gra… 4 oz   
#> 220 Fog Cutter              Rum - D…    904                 1 Light Rum  1 1/2 …
#> 221 Fog Cutter              Rum - D…    904                 2 Brandy     1/2 oz 
#> 222 Fog Cutter              Rum - D…    904                 3 Gin        1/2 oz 
#> 223 Fog Cutter              Rum - D…    904                 4 Fresh lem… 1 1/2 …
#> 224 Fog Cutter              Rum - D…    904                 5 each Oran… 1 oz   
#> 225 Fog Cutter              Rum - D…    904                 6 Almond or… 1 1/2 …
#> 226 Cherry Blossom Cocktail Cocktai…    914                 1 Old Mr. B… 1 1/2 …
#> 227 Cherry Blossom Cocktail Cocktai…    914                 2 Old Mr. B… 1/2 oz 
#> 228 Cherry Blossom Cocktail Cocktai…    914                 3 Old Mr. B… 1 1/2 …
#> 229 Cherry Blossom Cocktail Cocktai…    914                 4 Grenadine  1 1/2 …
#> 230 Cherry Blossom Cocktail Cocktai…    914                 5 Lemon Jui… 2 oz   
#> 231 Flower Power            Tequila     934                 1 Blanco te… 2 oz   
#> 232 Flower Power            Tequila     934                 2 Grapefrui… 1 oz   
#> 233 Flower Power            Tequila     934                 3 Agave nec… 1/2 oz 
#> 234 Flower Power            Tequila     934                 4 Egg White  1 oz   
#> 235 Flower Power            Tequila     934                 5 Orange bl… 3 oz   
#> 236 Flower Power            Tequila     934                 6 Peychaud'… 2 oz   
#> 237 El Ni_o                 Tequila     939                 1 Lime wedg… 6      
#> 238 El Ni_o                 Tequila     939                 2 Whole str… 1 oz   
#> 239 El Ni_o                 Tequila     939                 3 Simple Sy… 3/4 oz 
#> 240 El Ni_o                 Tequila     939                 4 Blanco te… 2 oz   
#> 241 El Ni_o                 Tequila     939                 5 Rose water 5 oz   
#> 242 Catalina Margarita      Tequila     951                 1 Blanco te… 1 1/2 …
#> 243 Catalina Margarita      Tequila     951                 2 Peach sch… 1 oz   
#> 244 Catalina Margarita      Tequila     951                 3 Blue cura… 1 oz   
#> 245 Catalina Margarita      Tequila     951                 4 Simple Sy… 2 oz   
#> 246 Catalina Margarita      Tequila     951                 5 Fresh lem… 2 oz   
#> 247 Amante Picante          Tequila     963                 1 Cucumber   2 oz   
#> 248 Amante Picante          Tequila     963                 2 Fresh cil… 2 oz   
#> 249 Amante Picante          Tequila     963                 3 Blanco te… 2 oz   
#> 250 Amante Picante          Tequila     963                 4 Fresh Lim… 1 oz   
#> 251 Amante Picante          Tequila     963                 5 Simple Sy… 1 oz   
#> 252 Amante Picante          Tequila     963                 6 Jalapeno … 2 oz   
#> 253 The Winkle              Gin         975                 1 Fresh sag… 3 oz   
#> 254 The Winkle              Gin         975                 2 Gin        2 oz   
#> 255 The Winkle              Gin         975                 3 Fresh Lim… 1 oz   
#> 256 The Winkle              Gin         975                 4 Limoncello 1/2 oz 
#> 257 The Winkle              Gin         975                 5 Simple Sy… 1/2 oz 
#> 258 The Winkle              Gin         975                 6 Raspberri… 4 oz

cocktails %>%
  filter(str_detect(ingredient, "bitters"))
#> # A tibble: 21 × 6
#>    name                  category row_id ingredient_number ingredient    measure
#>    <chr>                 <chr>     <dbl>             <dbl> <chr>         <chr>  
#>  1 High Cotton           Whiskies    129                 4 Peach bitters 2 oz   
#>  2 Jack Maples           Brandy      154                 3 Aromatic bit… 1 oz   
#>  3 Gilchrist             Whiskies    185                 5 Grapefruit b… 2 oz   
#>  4 Trident               Vodka       201                 4 Peach bitters 2 oz   
#>  5 Gotham                Brandy      239                 3 Peach bitters 3 oz   
#>  6 Cotton Club Flip      Whiskies    248                 5 Rhubarb bitt… 2 oz   
#>  7 Character Development Whiskies    266                 4 Grapefruit b… 2 oz   
#>  8 Academic Review       Whiskies    426                 6 Mole bitters… 2 oz   
#>  9 El Profesor           Brandy      513                 4 Aromatic bit… 2 oz   
#> 10 Georgia Mule          Vodka       542                 4 Peach bitters 2 oz   
#> # ℹ 11 more rows

sizes %>%
  group_by(ingredient) %>%
  summarise(n = n(), oz = mean(oz)) %>%
  filter(n > 5) %>%
  arrange(desc(oz), sort = TRUE)
#> # A tibble: 96 × 3
#>    ingredient               n    oz
#>    <chr>                <int> <dbl>
#>  1 Bourbon whiskey         38  1.79
#>  2 Bourbon                 11  1.70
#>  3 Anejo tequila            6  1.67
#>  4 Irish Whiskey           15  1.65
#>  5 Blended whiskey         18  1.61
#>  6 Straight rye whiskey    22  1.60
#>  7 Reposado tequila        10  1.6 
#>  8 Cognac                   6  1.58
#>  9 Dark rum                27  1.57
#> 10 Mr. Boston Gin           8  1.57
#> # ℹ 86 more rows


# What are the primary ingredients ----------------------------------------

cocktails <- cocktails %>% mutate(ingredient = tolower(ingredient))

cocktails %>%
  count(ingredient = tolower(ingredient), sort = TRUE) %>%
  head(20)
#> # A tibble: 20 × 2
#>    ingredient            n
#>    <chr>             <int>
#>  1 gin                 176
#>  2 fresh lemon juice   138
#>  3 simple syrup        115
#>  4 vodka               114
#>  5 light rum           113
#>  6 dry vermouth        107
#>  7 fresh lime juice    107
#>  8 triple sec          107
#>  9 powdered sugar       92
#> 10 grenadine            85
#> 11 sweet vermouth       84
#> 12 brandy               80
#> 13 lemon juice          70
#> 14 blanco tequila       69
#> 15 egg white            67
#> 16 angostura bitters    64
#> 17 juice of a lemon     60
#> 18 orange juice         57
#> 19 pineapple juice      47
#> 20 bourbon whiskey      40

# Hadley in 2021

standard_ingredients <- tribble(
  ~ingredient,         ~standard_name,
  "fresh lemon juice", "lemon juice",
  "juice of a lemon",  "lemon juice",
  "fresh lime juice",  "lime juice",
  "juice of a lime",   "lime juice",
  "bitters",           "angostura bitters"
)

ingredient_changes <- cocktails %>%
  select(name, ingredient_number, ingredient) %>%
  right_join(standard_ingredients) %>%
  select(name, ingredient_number, ingredient = standard_name)
#> Joining with `by = join_by(ingredient)`

cocktails %>%
  rows_update(ingredient_changes, by = c("name", "ingredient_number")) %>%
  count(ingredient, sort = TRUE) %>%
  head(20)
#> # A tibble: 20 × 2
#>    ingredient            n
#>    <chr>             <int>
#>  1 lemon juice         268
#>  2 gin                 176
#>  3 lime juice          153
#>  4 simple syrup        115
#>  5 vodka               114
#>  6 light rum           113
#>  7 dry vermouth        107
#>  8 triple sec          107
#>  9 angostura bitters    96
#> 10 powdered sugar       92
#> 11 grenadine            85
#> 12 sweet vermouth       84
#> 13 brandy               80
#> 14 blanco tequila       69
#> 15 egg white            67
#> 16 orange juice         57
#> 17 pineapple juice      47
#> 18 bourbon whiskey      40
#> 19 orange bitters       38
#> 20 whole egg            31


# New version using the replace_values function.

cocktails |>
  mutate(
    ingredient = replace_values(
      ingredient,
      c("fresh lemon juice", "juice of a lemon") ~ "lemon juice",
      c("fresh lime juice", "juice of a lime") ~ "lime juice",
      c("bitters") ~ "angostura bitters"
    )
  ) |>
  count(ingredient, sort = TRUE) |>
  head(20)
#> # A tibble: 20 × 2
#>    ingredient            n
#>    <chr>             <int>
#>  1 lemon juice         268
#>  2 gin                 176
#>  3 lime juice          153
#>  4 simple syrup        115
#>  5 vodka               114
#>  6 light rum           113
#>  7 dry vermouth        107
#>  8 triple sec          107
#>  9 angostura bitters    96
#> 10 powdered sugar       92
#> 11 grenadine            85
#> 12 sweet vermouth       84
#> 13 brandy               80
#> 14 blanco tequila       69
#> 15 egg white            67
#> 16 orange juice         57
#> 17 pineapple juice      47
#> 18 bourbon whiskey      40
#> 19 orange bitters       38
#> 20 whole egg            31

Created on 2025-09-18 with reprex v2.1.1.9000

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