forked from aedobbyn/beer-data-science
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_from_db.R
50 lines (30 loc) · 1.58 KB
/
read_from_db.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# read from db
library(RMySQL)
library(tidyverse)
# drv <- dbDriver("RMySQL")
con <- dbConnect(RMySQL::MySQL(), dbname="brewery_db", host='localhost', port=3306, user="root")
beer_necessities <- dbReadTable(con, "beer_necessities")
# set types
beer_necessities$style <- factor(beer_necessities$style)
beer_necessities$styleId <- factor(beer_necessities$styleId)
beer_necessities$glass <- factor(beer_necessities$glass)
beer_necessities$ibu <- as.numeric(beer_necessities$ibu)
beer_necessities$srm <- as.numeric(beer_necessities$srm)
beer_necessities$abv <- as.numeric(beer_necessities$abv)
beer_necessities$style_collapsed <- factor(beer_necessities$style_collapsed)
beer_necessities$hops_name <- factor(beer_necessities$hops_name)
beer_necessities$malt_name <- factor(beer_necessities$malt_name)
beer_necessities$hops_id <- factor(beer_necessities$hops_id)
beer_necessities$malt_id <- factor(beer_necessities$malt_id)
factorize_ingredients <- function(df) {
for(col_name in names(df)) {
if (grepl(("hops_name_|malt_name_"), col_name) == TRUE) {
df[[col_name]] <- factor(df[[col_name]])
}
}
return(df)
}
beer_necessities <- factorize_ingredients(beer_necessities)
keywords <- c("Lager", "Pale Ale", "India Pale Ale", "Double India Pale Ale", "India Pale Lager", "Hefeweizen", "Barrel-Aged","Wheat", "Pilsner", "Pilsener", "Amber", "Golden", "Blonde", "Brown", "Black", "Stout", "Porter", "Red", "Sour", "Kölsch", "Tripel", "Bitter", "Saison", "Strong Ale", "Barley Wine", "Dubbel", "Altbier")
# ------ read in from csv
# beer_necessities <- read_csv("./beer_necessities.csv")