R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

#Load the Library

library(tidyverse)
## ── Attaching packages ──────────────────────────────── tidyverse 1.3.0 ──
## ✔ ggplot2 3.2.1     ✔ purrr   0.3.3
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   1.0.0     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ─────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

Data source taken from Kaggle.

Overview of data:

Columns brewery_nameName of brewery typeType of establishment addressBrewery Address websiteWebsite address for the brewery stateState the brewery is located in state_breweriesNumber of breweries in this state

https://www.kaggle.com/brkurzawa/us-breweries

Read the data using readr function

brewery <- read_csv("https://raw.githubusercontent.com/jrovalino/data607-tidyverse/master/breweries_us.csv")
## Parsed with column specification:
## cols(
##   brewery_name = col_character(),
##   type = col_character(),
##   address = col_character(),
##   website = col_character(),
##   state = col_character(),
##   state_breweries = col_double()
## )
brewery
## # A tibble: 2,407 x 6
##    brewery_name      type    address          website      state state_breweries
##    <chr>             <chr>   <chr>            <chr>        <chr>           <dbl>
##  1 Valley Brewing C… Brewpub PO Box 4653, St… http://www.… cali…             284
##  2 Valley Brewing C… Brewpub 157 Adams St., … http://www.… cali…             284
##  3 Valley Brewing Co Microb… 1950 W Freemont… http://www.… cali…             284
##  4 Ukiah Brewing Co… Brewpub 102 S. State St… http://www.… cali…             284
##  5 Tustin Brewing C… Brewpub 13011 Newport A… http://www.… cali…             284
##  6 Trumer Brauerei   Microb… 1404 4th St., B… http://www.… cali…             284
##  7 Trumer Brauerei   Region… 1404 Fourth St.… http://www.… cali…             284
##  8 Triple Rock Brew… Brewpub 1920 Shattuck A… http://www.… cali…             284
##  9 Tied House Cafe … Brewpub 65 N. San Pedro… http://www.… cali…             284
## 10 Tied House Cafe … Brewpub 954 Villa St., … http://www.… cali…             284
## # … with 2,397 more rows
brewery <- as.tibble(brewery)
## Warning: `as.tibble()` is deprecated, use `as_tibble()` (but mind the new semantics).
## This warning is displayed once per session.
head(brewery)
## # A tibble: 6 x 6
##   brewery_name    type    address           website       state  state_breweries
##   <chr>           <chr>   <chr>             <chr>         <chr>            <dbl>
## 1 Valley Brewing… Brewpub PO Box 4653, Sto… http://www.v… calif…             284
## 2 Valley Brewing… Brewpub 157 Adams St., S… http://www.v… calif…             284
## 3 Valley Brewing… Microb… 1950 W Freemont,… http://www.v… calif…             284
## 4 Ukiah Brewing … Brewpub 102 S. State St.… http://www.u… calif…             284
## 5 Tustin Brewing… Brewpub 13011 Newport Av… http://www.t… calif…             284
## 6 Trumer Brauerei Microb… 1404 4th St., Be… http://www.t… calif…             284

1) Dplyr:Sample_n feature Tutorial

Description

Retrieves a random sample

Usage

sample_n(df, #)

Example

sample_brewery <- sample_n(brewery, 10)
sample_brewery
## # A tibble: 10 x 6
##    brewery_name       type    address         website     state  state_breweries
##    <chr>              <chr>   <chr>           <chr>       <chr>            <dbl>
##  1 Bradley's Brewing… Microb… 8942 Greenback… http://www… calif…             284
##  2 Neptunes Brewery,… Microb… 119 North L st… http://www… monta…              32
##  3 John Harvards Bre… Brewpub 3466 William P… http://www… penns…             107
##  4 Deschutes Brewery… Brewpub 1044 Bond St. … http://www… oregon             156
##  5 Silver Moon Brewi… Microb… 24 Northwest G… http://www… oregon             156
##  6 BJ's Restaurant &… Brewpub 107 S. 1st St.… http://www… calif…             284
##  7 Greenshields Brew… Brewpub 214 E. Martin … -           north…              61
##  8 Judge Baldwin's B… Microb… 4 South Cascad… -           color…             182
##  9 Kroghs Restaurant… Brewpub 23 White Deer … http://www… new-j…              41
## 10 Italian Oasis Res… Brewpub 106 Main St., … -           new-h…              18

2) Dplyr: Filter Feature Tutorial

Description

Exact rows that meet criteria

Usage

filter(df, state = ‘new-york’)

Example

ny_brewery <- filter(brewery, state == 'new-york')
ny_brewery
## # A tibble: 107 x 6
##    brewery_name    type    address           website       state state_breweries
##    <chr>           <chr>   <chr>             <chr>         <chr>           <dbl>
##  1 Wagner Valley … Brewpub 9322 Route 414, … http://www.w… new-…             107
##  2 Van Dyck Resta… Brewpub 237 Union St., S… http://www.t… new-…             107
##  3 Typhoon Brewery Brewpu… 22 E. 54th St. (… -             new-…             107
##  4 Troy Brewing C… Brewpub 417-419 River St… http://www.b… new-…             107
##  5 The Riverosa C… Contra… 101 W. 75th St. … -             new-…             107
##  6 Syracuse Suds … Brewpub 320 S Clinton St… http://www.s… new-…             107
##  7 Spring Street … Microb… 113 University P… http://plaza… new-…             107
##  8 Southern Tier … Microb… PO Box 166, Lake… http://www.s… new-…             107
##  9 Southampton Pu… Brewpub 40 Bowden Sq., S… http://www.p… new-…             107
## 10 Skytop Steakho… Brewpub 30 Forest Hills … http://skyto… new-…             107
## # … with 97 more rows

3) Dplyr: Count() Feature Tutorial

Description

Count number of rows with each unique value of variable (with or without weights).

Usage

count(df, state = ‘new-york’)

Example

nycount <- count(brewery,state == 'new-york')
nycount
## # A tibble: 2 x 2
##   `state == "new-york"`     n
##   <lgl>                 <int>
## 1 FALSE                  2300
## 2 TRUE                    107

4) Dplyr: Summarize Feature Tutorial

Description

Compute separate summary row for each group.

Usage

summarize()

Example

stategrpbrew <- brewery %>% group_by(state,type) %>% summarize()
stategrpbrew
## # A tibble: 234 x 2
## # Groups:   state [51]
##    state   type           
##    <chr>   <chr>          
##  1 alabama Brewpub        
##  2 alabama Contract       
##  3 alabama ContractBrewery
##  4 alabama Microbrewery   
##  5 alaska  Brewpub        
##  6 alaska  Microbrewery   
##  7 alaska  Mircobrewery   
##  8 arizona Brewpub        
##  9 arizona Brewpub-Closed 
## 10 arizona ContractBrewery
## # … with 224 more rows