Community
Community
Ecosystem
Old Code vs. New Code
Calling Multiple Functions
Get the fourth row of a data.frame with just head() and tail()
tail(head(iris, n=4), n=1)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species 4 4.6 3.1 1.5 0.2 setosa
iris %>% head(n=4) %>% tail(n=1)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species 4 4.6 3.1 1.5 0.2 setosa
Working Directories
list.files('../../data')
[1] "acs_ny.csv" [2] "ADS.xdf" [3] "AdventureWorks.db" [4] "allGames.csv" [5] "att.csv" [6] "att.rdata" [7] "batting.sqlite3" [8] "citibike_201307_201707.csv" [9] "citibike_201708_201709.csv" [10] "citibike_stations.csv" [11] "cran_releases_2019-04-15.csv" [12] "crean_releases_20190507.csv" [13] "credit.rdata" [14] "credit2.rdata" [15] "diamonds.db" [16] "diamonds.xdf" [17] "djia.rdata" [18] "dow.rdata" [19] "econ_long.rds" [20] "econ_ts.rds" [21] "econ_wide.rds" [22] "econ_xts.rds" [23] "eliPage.html" [24] "Employees.csv" [25] "football" [26] "gamesLoaded.rdata" [27] "google.rds" [28] "groupInfo.rdata" [29] "gTag.rds" [30] "holidays_2013_2017.csv" [31] "housing.csv" [32] "ideo.rdata" [33] "land.csv" [34] "land.rds" [35] "Leads.csv" [36] "loans_Test.csv" [37] "loans_Train.csv" [38] "loans_Validate.csv" [39] "meetup_member_list_2019-04-11.tsv" [40] "meetup_member_list_2019-05-07.tsv" [41] "NFLPlayers.rds" [42] "Offices.csv" [43] "Opportunities.csv" [44] "Pizza Consolidated.csv" [45] "pizza_reviews.tsv" [46] "PizzaHealthScores.csv" [47] "PizzaPlaces.csv" [48] "PizzaPlacesSample.csv" [49] "players2016.rds" [50] "records.dat" [51] "SalesLeads.csv" [52] "SalesLeads_New.csv" [53] "stocks.rdata" [54] "tibco.rdata" [55] "tickers.rdata" [56] "TomatoFirst.csv" [57] "wine.csv"
here('data') %>% dir_ls()
C:/Users/jared/Documents/Consulting/talks/data/acs_ny.csv C:/Users/jared/Documents/Consulting/talks/data/ADS.xdf C:/Users/jared/Documents/Consulting/talks/data/AdventureWorks.db C:/Users/jared/Documents/Consulting/talks/data/allGames.csv C:/Users/jared/Documents/Consulting/talks/data/att.csv C:/Users/jared/Documents/Consulting/talks/data/att.rdata C:/Users/jared/Documents/Consulting/talks/data/batting.sqlite3 C:/Users/jared/Documents/Consulting/talks/data/citibike_201307_201707.csv C:/Users/jared/Documents/Consulting/talks/data/citibike_201708_201709.csv C:/Users/jared/Documents/Consulting/talks/data/citibike_stations.csv C:/Users/jared/Documents/Consulting/talks/data/cran_releases_2019-04-15.csv C:/Users/jared/Documents/Consulting/talks/data/crean_releases_20190507.csv C:/Users/jared/Documents/Consulting/talks/data/credit.rdata C:/Users/jared/Documents/Consulting/talks/data/credit2.rdata C:/Users/jared/Documents/Consulting/talks/data/diamonds.db C:/Users/jared/Documents/Consulting/talks/data/diamonds.xdf C:/Users/jared/Documents/Consulting/talks/data/djia.rdata C:/Users/jared/Documents/Consulting/talks/data/dow.rdata C:/Users/jared/Documents/Consulting/talks/data/econ_long.rds C:/Users/jared/Documents/Consulting/talks/data/econ_ts.rds C:/Users/jared/Documents/Consulting/talks/data/econ_wide.rds C:/Users/jared/Documents/Consulting/talks/data/econ_xts.rds C:/Users/jared/Documents/Consulting/talks/data/eliPage.html C:/Users/jared/Documents/Consulting/talks/data/Employees.csv C:/Users/jared/Documents/Consulting/talks/data/football C:/Users/jared/Documents/Consulting/talks/data/gamesLoaded.rdata C:/Users/jared/Documents/Consulting/talks/data/google.rds C:/Users/jared/Documents/Consulting/talks/data/groupInfo.rdata C:/Users/jared/Documents/Consulting/talks/data/gTag.rds C:/Users/jared/Documents/Consulting/talks/data/holidays_2013_2017.csv C:/Users/jared/Documents/Consulting/talks/data/housing.csv C:/Users/jared/Documents/Consulting/talks/data/ideo.rdata C:/Users/jared/Documents/Consulting/talks/data/land.csv C:/Users/jared/Documents/Consulting/talks/data/land.rds C:/Users/jared/Documents/Consulting/talks/data/Leads.csv C:/Users/jared/Documents/Consulting/talks/data/loans_Test.csv C:/Users/jared/Documents/Consulting/talks/data/loans_Train.csv C:/Users/jared/Documents/Consulting/talks/data/loans_Validate.csv C:/Users/jared/Documents/Consulting/talks/data/meetup_member_list_2019-04-11.tsv C:/Users/jared/Documents/Consulting/talks/data/meetup_member_list_2019-05-07.tsv C:/Users/jared/Documents/Consulting/talks/data/NFLPlayers.rds C:/Users/jared/Documents/Consulting/talks/data/Offices.csv C:/Users/jared/Documents/Consulting/talks/data/Opportunities.csv C:/Users/jared/Documents/Consulting/talks/data/Pizza Consolidated.csv C:/Users/jared/Documents/Consulting/talks/data/PizzaHealthScores.csv C:/Users/jared/Documents/Consulting/talks/data/PizzaPlaces.csv C:/Users/jared/Documents/Consulting/talks/data/PizzaPlacesSample.csv C:/Users/jared/Documents/Consulting/talks/data/pizza_reviews.tsv C:/Users/jared/Documents/Consulting/talks/data/players2016.rds C:/Users/jared/Documents/Consulting/talks/data/records.dat C:/Users/jared/Documents/Consulting/talks/data/SalesLeads.csv C:/Users/jared/Documents/Consulting/talks/data/SalesLeads_New.csv C:/Users/jared/Documents/Consulting/talks/data/stocks.rdata C:/Users/jared/Documents/Consulting/talks/data/tibco.rdata C:/Users/jared/Documents/Consulting/talks/data/tickers.rdata C:/Users/jared/Documents/Consulting/talks/data/TomatoFirst.csv C:/Users/jared/Documents/Consulting/talks/data/wine.csv
Reading Data
pizza_base <- read.csv('../../data/PizzaPlacesSample.csv')
pizza_base
Name Price Address
1 Rocky's 1 474 Coney Island Ave
2 La Lanterna Caffe 3 129 MacDougal St
3 L Bella Pizzeria 3 69-26 Main St
4 Cheesy Pizza 2 2640 Broadway
5 Little Italy Pizza 2 11 Park Pl
6 Pomodoro Pizza 2 14 Rector St
7 Broadway Gourmet 1 588 Broadway
8 Antonio's Pizza 1 318 Flatbush Ave
9 La Bruschetta 3 256 7th Ave
10 Como Pizza 2 4035 Broadway
11 Bella Napoli 3 150 W 49th St
12 Paulie Gee's 3 60 Greenpoint Ave
13 Aurora Pizza 1 7624 17th Ave
14 Amsterdam Ale House 3 340 Amsterdam Ave
15 Ozone Pizzeria 2 96-15 Liberty Ave
16 Angelo's Pizza 2 57-64 Maspeth Ave
17 Armonie 3 100 E 117th St
18 Pizza World 1 1387 Coney Island Ave
19 Grandaisy Bakery 2 250 W Broadway
20 Party Pizza 2 86-14 37th Ave
21 Cosi 2 55 Broad St
22 Off Broadway Deli 2 726 8th Ave
23 Angelica Pizza 1 30 Nevins St
24 EvilOlivE Pizza Bar 1 198 Union Ave
25 TiramiSu 3 1410 3rd Ave
26 Le Paddock 3 1235 Prospect Ave
27 Van Diemens 2 383 3rd Ave
28 Tommy's 2 3750 Broadway
29 Little Munchies Pizzeria 1 734 Evergreen Ave
30 Gino's Pizzeria 2 218 Flatbush Ave
31 B&W Deli and Pizzeria 1 373 W 34th St
32 Vito's Pizza 3 78-37 Springfield Blvd
33 Fat Sal's Pizza 2 1603 2nd Ave
34 T & R Pizza 2 411 Amsterdam Ave
35 Broadway Pizza 1 1157 Broadway
36 Astoria Park Pizzeria 2 19-27 Ditmars Blvd
37 Dagan Kosher Pizza 3 6187 Strickland Ave
38 Serafina Always 4 33 E 61st St
39 Adriana Pizzeria 2 253 3rd Ave
40 Don Giuseppe Trattoria / Ray Bari Pizza 3 176-61 Union Tpke
41 Benny's Pizza Place 2 9102 4th Ave
42 Hudson Market 2 111 Hudson St
43 Prima Pizza 2 222-70 Braddock Ave
44 Due Fratelli 3 101-16 Queens Blvd
45 City 75 1 75 Rockefeller Plaza
46 Two Sisters Pizza 1 232 W 37th St
47 Mamma's Famous Ristorante 2 2345 Broadway
48 Monty Q's 3 158 Montague St
49 Blinks Deli & Pizzeria 2 43-01 35th St
50 Coco's 1 1377A Sutter Ave
51 Cafe Basil 2 762 3rd Ave
52 Roy's Pizza 2 154 8th Ave
53 Rivoli Pizza 1 176 7th Ave S
54 Golosi 2 1304A 2nd Ave
55 Pizzabolla 1 35 West End Ave
56 Luigi's 2 1615 Avenue U
57 Famous Ben's Pizza 2 177 Spring St
58 Famous Original Ray's Pizza 2 462 Columbus Ave
59 Saluggi's 3 325 Church St
60 Times Square Food Court 2 263 W 42nd St
61 Pizza House 2 1142 Broadway
62 J & V Pizzeria 1 6322 18th Ave
63 K Food Pizza 2 18 E 33rd St
64 Dreamer's Pizza 3 1850 3rd Ave
65 Knapp Pizza III 1 427 Kings Hwy
66 99 Cent Express Pizza 1 301 W 43rd St
67 Il Fornetto 4 2902 Emmons Ave
68 Naomi's Kosher Pizza & Israel Falafel 1 68-28 Main St
69 Taste Of Italian 2 84-07 Jamaica Ave
70 Angelo's Pizzeria 2 39-03 103rd St
71 Trattoria L'Incontro 4 21-76 31st St
72 Teresa's Gourmet Coffee Bar 1 51 W 51st St
73 The Brooklyn Pizza Factory 2 663 Vanderbilt Ave
74 Salvo's Pizza Bar 3 1477 York Ave
75 Frank's Pizzeria 2 7502 13th Ave
76 Gigi's Pizza 2 20-09 Francis Lewis Blvd
77 Tato's Pizza 3 8409 18th Ave
78 Osteria Cotta 3 513 Columbus Ave
79 Pia's Pizzeria 2 3682 Nostrand Ave #A
80 Mezzaluna 4 1295 3rd Ave
81 Patsy's Pizzeria 3 318 W 23rd St
82 Tony's Pizza 3 850 Nostrand Ave
83 Krust Pizzeria 2 226 E 14th St
84 Mamani 1 151 Avenue A
85 Italy Pizza 2 800 Manhattan Ave
86 Pizza King 1 1013 6th Ave
87 Beny's Pizza 1 4514 13th Ave
88 Tri-Color Restaurant & Pizzeria 2 245 Graham Ave
89 Rafele 4 29 7th Ave S
90 Table 87 2 87 Atlantic Ave
91 Eleven B 3 174 Ave B
92 Famous Famiglia 2 734 Amsterdam Ave
93 Los Cuencanitos 2 54-18 Roosevelt Ave
94 South Brooklyn Pizza 2 451 Court St
95 Squisito 3 64 E 34th St
96 Armando's Pizza 2 1413 Rockaway Pkwy
97 Pete's Place 2 5620 1st Ave
98 Adrienne's Pizza Bar 3 54 Stone St
99 Cafe Viva 2 2578 Broadway
100 Steves Pizza Place 2 82-18 Eliot Ave
City State Zip Phone
1 Brooklyn NY 11218 (718) 854-9270
2 New York NY 10012 (212) 529-5945
3 Flushing NY 11367 (718) 261-0196
4 New York NY 10025 (212) 662-5223
5 New York NY 10007 (212) 227-7077
6 New York NY 10006 (212) 406-0917
7 New York NY 10012 (212) 941-6566
8 Brooklyn NY 11238 (718) 398-2300
9 Brooklyn NY 11215 (718) 499-4994
10 New York NY 10032 (212) 928-7867
11 New York NY 10019 (212) 719-2819
12 Brooklyn NY 11222 (347) 987-3747
13 Brooklyn NY 11214 (718) 259-5260
14 New York NY 10023 (212) 362-7260
15 Ozone Park NY 11417 (718) 845-9555
16 Maspeth NY 11378 (718) 326-5757
17 New York NY 10029 (212) 722-6400
18 Brooklyn NY 11230 (718) 758-3572
19 New York NY 10013 (212) 334-9435
20 Jackson Heights NY 11372 (718) 651-3050
21 New York NY 10004 (212) 344-5000
22 New York NY 10036 (212) 398-3981
23 Brooklyn NY 11217 (718) 852-2728
24 Brooklyn NY 11206 (718) 387-0707
25 New York NY 10028 (212) 988-9780
26 Brooklyn NY 11218 (718) 435-0921
27 New York NY 10016 (212) 532-1123
28 New York NY 10032 (212) 368-1837
29 Brooklyn NY 11207 (718) 455-6555
30 Brooklyn NY 11217 (718) 230-3932
31 New York NY 10001 (917) 388-2601
32 Oakland Gardens NY 11364 (718) 468-1019
33 New York NY 10028 (212) 744-4700
34 New York NY 10024 (212) 787-4093
35 New York NY 10001 (212) 679-4633
36 Astoria NY 11105 (718) 721-9722
37 Brooklyn NY 11234 (718) 209-0636
38 New York NY 10065 (212) 702-9898
39 New York NY 10010 (212) 674-6070
40 Fresh Meadows NY 11366 (718) 591-7901
41 Brooklyn NY 11209 (718) 745-5500
42 New York NY 10013 (212) 226-9310
43 Queens Village NY 11428 (718) 740-6546
44 Forest Hills NY 11375 (718) 997-7111
45 New York NY 10019 (212) 247-1500
46 New York NY 10018 (646) 666-0096
47 New York NY 10024 (212) 362-8612
48 Brooklyn NY 11201 (718) 246-2000
49 Long Island City NY 11101 (718) 392-6155
50 Brooklyn NY 11208 (718) 348-0585
51 New York NY 10017 (212) 758-3700
52 New York NY 10011 (212) 229-1810
53 New York NY 10014 (212) 691-2704
54 New York NY 10065 (212) 772-1169
55 New York NY 10023 (212) 586-2000
56 Brooklyn NY 11229 (718) 382-9100
57 New York NY 10012 (212) 966-4494
58 New York NY 10024 (212) 873-1134
59 New York NY 10013 (212) 226-7900
60 New York NY 10036 (212) 302-2941
61 Brooklyn NY 11221 (718) 602-6110
62 Brooklyn NY 11204 (718) 232-2700
63 New York NY 10016 (212) 213-5100
64 New York NY 10029 (212) 987-5414
65 Brooklyn NY 11229 (718) 627-1700
66 New York NY 10036 (212) 315-5257
67 Brooklyn NY 11235 (718) 332-8494
68 Flushing NY 11367 (718) 520-8754
69 Woodhaven NY 11421 (718) 805-8190
70 Corona NY 11368 (718) 335-8637
71 Astoria NY 11105 (718) 721-3532
72 New York NY 10019 (212) 265-7988
73 Brooklyn NY 11238 (718) 789-7800
74 New York NY 10075 (646) 590-1925
75 Brooklyn NY 11228 (718) 680-4444
76 Whitestone NY 11357 (718) 352-9200
77 Brooklyn NY 11214 (718) 621-3300
78 New York NY 10024 (212) 873-8500
79 Brooklyn NY 11229 (718) 646-6786
80 New York NY 10021 (212) 535-9600
81 New York NY 10011 (646) 486-7400
82 Brooklyn NY 11225 (718) 363-8300
83 New York NY 10003 (212) 510-8869
84 New York NY 10009 (212) 388-1715
85 Brooklyn NY 11222 (718) 383-9079
86 New York NY 10018 (212) 840-3767
87 Brooklyn NY 11219 (718) 438-2369
88 Brooklyn NY 11211 (718) 599-2523
89 New York NY 10014 (212) 242-1999
90 New York NY 11201 (718) 797-9300
91 New York NY 10009 (212) 388-9811
92 New York NY 10025 (212) 749-1111
93 Woodside NY 11377 (718) 426-1734
94 Brooklyn NY 11231 (718) 852-4038
95 New York NY 10016 (212) 779-1400
96 Brooklyn NY 11236 (718) 257-9723
97 Brooklyn NY 11220 (718) 680-5858
98 New York NY 10004 (212) 248-3838
99 New York NY 10025 (212) 663-8482
100 Middle Village NY 11379 (718) 899-4751
class(pizza_base$Name)
[1] "factor"
pizza_modern <- read_csv(here('data', 'PizzaPlacesSample.csv'))
Parsed with column specification: cols( Name = col_character(), Price = col_double(), Address = col_character(), City = col_character(), State = col_character(), Zip = col_double(), Phone = col_character() )
pizza_modern
# A tibble: 100 x 7 Name Price Address City State Zip Phone <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> 1 Rocky's 1 474 Coney Island~ Brooklyn NY 11218 (718) 854-~ 2 La Lanterna Ca~ 3 129 MacDougal St New York NY 10012 (212) 529-~ 3 L Bella Pizzer~ 3 69-26 Main St Flushing NY 11367 (718) 261-~ 4 Cheesy Pizza 2 2640 Broadway New York NY 10025 (212) 662-~ 5 Little Italy P~ 2 11 Park Pl New York NY 10007 (212) 227-~ 6 Pomodoro Pizza 2 14 Rector St New York NY 10006 (212) 406-~ 7 Broadway Gourm~ 1 588 Broadway New York NY 10012 (212) 941-~ 8 Antonio's Pizza 1 318 Flatbush Ave Brooklyn NY 11238 (718) 398-~ 9 La Bruschetta 3 256 7th Ave Brooklyn NY 11215 (718) 499-~ 10 Como Pizza 2 4035 Broadway New York NY 10032 (212) 928-~ # ... with 90 more rows
pizza_modern$Name %>% class()
[1] "character"
Getting Columns
subset(pizza_base, select=c(Name, Price))
Name Price 1 Rocky's 1 2 La Lanterna Caffe 3 3 L Bella Pizzeria 3 4 Cheesy Pizza 2 5 Little Italy Pizza 2 6 Pomodoro Pizza 2 7 Broadway Gourmet 1 8 Antonio's Pizza 1 9 La Bruschetta 3 10 Como Pizza 2 11 Bella Napoli 3 12 Paulie Gee's 3 13 Aurora Pizza 1 14 Amsterdam Ale House 3 15 Ozone Pizzeria 2 16 Angelo's Pizza 2 17 Armonie 3 18 Pizza World 1 19 Grandaisy Bakery 2 20 Party Pizza 2 21 Cosi 2 22 Off Broadway Deli 2 23 Angelica Pizza 1 24 EvilOlivE Pizza Bar 1 25 TiramiSu 3 26 Le Paddock 3 27 Van Diemens 2 28 Tommy's 2 29 Little Munchies Pizzeria 1 30 Gino's Pizzeria 2 31 B&W Deli and Pizzeria 1 32 Vito's Pizza 3 33 Fat Sal's Pizza 2 34 T & R Pizza 2 35 Broadway Pizza 1 36 Astoria Park Pizzeria 2 37 Dagan Kosher Pizza 3 38 Serafina Always 4 39 Adriana Pizzeria 2 40 Don Giuseppe Trattoria / Ray Bari Pizza 3 41 Benny's Pizza Place 2 42 Hudson Market 2 43 Prima Pizza 2 44 Due Fratelli 3 45 City 75 1 46 Two Sisters Pizza 1 47 Mamma's Famous Ristorante 2 48 Monty Q's 3 49 Blinks Deli & Pizzeria 2 50 Coco's 1 51 Cafe Basil 2 52 Roy's Pizza 2 53 Rivoli Pizza 1 54 Golosi 2 55 Pizzabolla 1 56 Luigi's 2 57 Famous Ben's Pizza 2 58 Famous Original Ray's Pizza 2 59 Saluggi's 3 60 Times Square Food Court 2 61 Pizza House 2 62 J & V Pizzeria 1 63 K Food Pizza 2 64 Dreamer's Pizza 3 65 Knapp Pizza III 1 66 99 Cent Express Pizza 1 67 Il Fornetto 4 68 Naomi's Kosher Pizza & Israel Falafel 1 69 Taste Of Italian 2 70 Angelo's Pizzeria 2 71 Trattoria L'Incontro 4 72 Teresa's Gourmet Coffee Bar 1 73 The Brooklyn Pizza Factory 2 74 Salvo's Pizza Bar 3 75 Frank's Pizzeria 2 76 Gigi's Pizza 2 77 Tato's Pizza 3 78 Osteria Cotta 3 79 Pia's Pizzeria 2 80 Mezzaluna 4 81 Patsy's Pizzeria 3 82 Tony's Pizza 3 83 Krust Pizzeria 2 84 Mamani 1 85 Italy Pizza 2 86 Pizza King 1 87 Beny's Pizza 1 88 Tri-Color Restaurant & Pizzeria 2 89 Rafele 4 90 Table 87 2 91 Eleven B 3 92 Famous Famiglia 2 93 Los Cuencanitos 2 94 South Brooklyn Pizza 2 95 Squisito 3 96 Armando's Pizza 2 97 Pete's Place 2 98 Adrienne's Pizza Bar 3 99 Cafe Viva 2 100 Steves Pizza Place 2
?subset
Warning
This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.
pizza_base[, c('Name', 'Price')]
Name Price 1 Rocky's 1 2 La Lanterna Caffe 3 3 L Bella Pizzeria 3 4 Cheesy Pizza 2 5 Little Italy Pizza 2 6 Pomodoro Pizza 2 7 Broadway Gourmet 1 8 Antonio's Pizza 1 9 La Bruschetta 3 10 Como Pizza 2 11 Bella Napoli 3 12 Paulie Gee's 3 13 Aurora Pizza 1 14 Amsterdam Ale House 3 15 Ozone Pizzeria 2 16 Angelo's Pizza 2 17 Armonie 3 18 Pizza World 1 19 Grandaisy Bakery 2 20 Party Pizza 2 21 Cosi 2 22 Off Broadway Deli 2 23 Angelica Pizza 1 24 EvilOlivE Pizza Bar 1 25 TiramiSu 3 26 Le Paddock 3 27 Van Diemens 2 28 Tommy's 2 29 Little Munchies Pizzeria 1 30 Gino's Pizzeria 2 31 B&W Deli and Pizzeria 1 32 Vito's Pizza 3 33 Fat Sal's Pizza 2 34 T & R Pizza 2 35 Broadway Pizza 1 36 Astoria Park Pizzeria 2 37 Dagan Kosher Pizza 3 38 Serafina Always 4 39 Adriana Pizzeria 2 40 Don Giuseppe Trattoria / Ray Bari Pizza 3 41 Benny's Pizza Place 2 42 Hudson Market 2 43 Prima Pizza 2 44 Due Fratelli 3 45 City 75 1 46 Two Sisters Pizza 1 47 Mamma's Famous Ristorante 2 48 Monty Q's 3 49 Blinks Deli & Pizzeria 2 50 Coco's 1 51 Cafe Basil 2 52 Roy's Pizza 2 53 Rivoli Pizza 1 54 Golosi 2 55 Pizzabolla 1 56 Luigi's 2 57 Famous Ben's Pizza 2 58 Famous Original Ray's Pizza 2 59 Saluggi's 3 60 Times Square Food Court 2 61 Pizza House 2 62 J & V Pizzeria 1 63 K Food Pizza 2 64 Dreamer's Pizza 3 65 Knapp Pizza III 1 66 99 Cent Express Pizza 1 67 Il Fornetto 4 68 Naomi's Kosher Pizza & Israel Falafel 1 69 Taste Of Italian 2 70 Angelo's Pizzeria 2 71 Trattoria L'Incontro 4 72 Teresa's Gourmet Coffee Bar 1 73 The Brooklyn Pizza Factory 2 74 Salvo's Pizza Bar 3 75 Frank's Pizzeria 2 76 Gigi's Pizza 2 77 Tato's Pizza 3 78 Osteria Cotta 3 79 Pia's Pizzeria 2 80 Mezzaluna 4 81 Patsy's Pizzeria 3 82 Tony's Pizza 3 83 Krust Pizzeria 2 84 Mamani 1 85 Italy Pizza 2 86 Pizza King 1 87 Beny's Pizza 1 88 Tri-Color Restaurant & Pizzeria 2 89 Rafele 4 90 Table 87 2 91 Eleven B 3 92 Famous Famiglia 2 93 Los Cuencanitos 2 94 South Brooklyn Pizza 2 95 Squisito 3 96 Armando's Pizza 2 97 Pete's Place 2 98 Adrienne's Pizza Bar 3 99 Cafe Viva 2 100 Steves Pizza Place 2
pizza_modern[, c('Name', 'Price')]
# A tibble: 100 x 2 Name Price <chr> <dbl> 1 Rocky's 1 2 La Lanterna Caffe 3 3 L Bella Pizzeria 3 4 Cheesy Pizza 2 5 Little Italy Pizza 2 6 Pomodoro Pizza 2 7 Broadway Gourmet 1 8 Antonio's Pizza 1 9 La Bruschetta 3 10 Como Pizza 2 # ... with 90 more rows
pizza_base[, c('Price')]
[1] 1 3 3 2 2 2 1 1 3 2 3 3 1 3 2 2 3 1 2 2 2 2 1 1 3 3 2 2 1 2 1 3 2 2 1 [36] 2 3 4 2 3 2 2 2 3 1 1 2 3 2 1 2 2 1 2 1 2 2 2 3 2 2 1 2 3 1 1 4 1 2 2 [71] 4 1 2 3 2 2 3 3 2 4 3 3 2 1 2 1 1 2 4 2 3 2 2 2 3 2 2 3 2 2
pizza_base[, c('Price'), drop=FALSE]
Price 1 1 2 3 3 3 4 2 5 2 6 2 7 1 8 1 9 3 10 2 11 3 12 3 13 1 14 3 15 2 16 2 17 3 18 1 19 2 20 2 21 2 22 2 23 1 24 1 25 3 26 3 27 2 28 2 29 1 30 2 31 1 32 3 33 2 34 2 35 1 36 2 37 3 38 4 39 2 40 3 41 2 42 2 43 2 44 3 45 1 46 1 47 2 48 3 49 2 50 1 51 2 52 2 53 1 54 2 55 1 56 2 57 2 58 2 59 3 60 2 61 2 62 1 63 2 64 3 65 1 66 1 67 4 68 1 69 2 70 2 71 4 72 1 73 2 74 3 75 2 76 2 77 3 78 3 79 2 80 4 81 3 82 3 83 2 84 1 85 2 86 1 87 1 88 2 89 4 90 2 91 3 92 2 93 2 94 2 95 3 96 2 97 2 98 3 99 2 100 2
pizza_modern[, c('Price')]
# A tibble: 100 x 1 Price <dbl> 1 1 2 3 3 3 4 2 5 2 6 2 7 1 8 1 9 3 10 2 # ... with 90 more rows
pizza_modern[, c('Price'), drop=TRUE]
[1] 1 3 3 2 2 2 1 1 3 2 3 3 1 3 2 2 3 1 2 2 2 2 1 1 3 3 2 2 1 2 1 3 2 2 1 [36] 2 3 4 2 3 2 2 2 3 1 1 2 3 2 1 2 2 1 2 1 2 2 2 3 2 2 1 2 3 1 1 4 1 2 2 [71] 4 1 2 3 2 2 3 3 2 4 3 3 2 1 2 1 1 2 4 2 3 2 2 2 3 2 2 3 2 2
pizza_modern %>% select(Name, Price)
# A tibble: 100 x 2 Name Price <chr> <dbl> 1 Rocky's 1 2 La Lanterna Caffe 3 3 L Bella Pizzeria 3 4 Cheesy Pizza 2 5 Little Italy Pizza 2 6 Pomodoro Pizza 2 7 Broadway Gourmet 1 8 Antonio's Pizza 1 9 La Bruschetta 3 10 Como Pizza 2 # ... with 90 more rows
pizza_modern %>% select(Price)
# A tibble: 100 x 1 Price <dbl> 1 1 2 3 3 3 4 2 5 2 6 2 7 1 8 1 9 3 10 2 # ... with 90 more rows
pizza_modern %>% pull(Price)
[1] 1 3 3 2 2 2 1 1 3 2 3 3 1 3 2 2 3 1 2 2 2 2 1 1 3 3 2 2 1 2 1 3 2 2 1 [36] 2 3 4 2 3 2 2 2 3 1 1 2 3 2 1 2 2 1 2 1 2 2 2 3 2 2 1 2 3 1 1 4 1 2 2 [71] 4 1 2 3 2 2 3 3 2 4 3 3 2 1 2 1 1 2 4 2 3 2 2 2 3 2 2 3 2 2
pizza_modern %>% `[`(c('Name', 'Price'))
# A tibble: 100 x 2 Name Price <chr> <dbl> 1 Rocky's 1 2 La Lanterna Caffe 3 3 L Bella Pizzeria 3 4 Cheesy Pizza 2 5 Little Italy Pizza 2 6 Pomodoro Pizza 2 7 Broadway Gourmet 1 8 Antonio's Pizza 1 9 La Bruschetta 3 10 Como Pizza 2 # ... with 90 more rows
Conditionally Getting Rows
pizza_base[pizza_base$Price >= 3, ]
Name Price Address
2 La Lanterna Caffe 3 129 MacDougal St
3 L Bella Pizzeria 3 69-26 Main St
9 La Bruschetta 3 256 7th Ave
11 Bella Napoli 3 150 W 49th St
12 Paulie Gee's 3 60 Greenpoint Ave
14 Amsterdam Ale House 3 340 Amsterdam Ave
17 Armonie 3 100 E 117th St
25 TiramiSu 3 1410 3rd Ave
26 Le Paddock 3 1235 Prospect Ave
32 Vito's Pizza 3 78-37 Springfield Blvd
37 Dagan Kosher Pizza 3 6187 Strickland Ave
38 Serafina Always 4 33 E 61st St
40 Don Giuseppe Trattoria / Ray Bari Pizza 3 176-61 Union Tpke
44 Due Fratelli 3 101-16 Queens Blvd
48 Monty Q's 3 158 Montague St
59 Saluggi's 3 325 Church St
64 Dreamer's Pizza 3 1850 3rd Ave
67 Il Fornetto 4 2902 Emmons Ave
71 Trattoria L'Incontro 4 21-76 31st St
74 Salvo's Pizza Bar 3 1477 York Ave
77 Tato's Pizza 3 8409 18th Ave
78 Osteria Cotta 3 513 Columbus Ave
80 Mezzaluna 4 1295 3rd Ave
81 Patsy's Pizzeria 3 318 W 23rd St
82 Tony's Pizza 3 850 Nostrand Ave
89 Rafele 4 29 7th Ave S
91 Eleven B 3 174 Ave B
95 Squisito 3 64 E 34th St
98 Adrienne's Pizza Bar 3 54 Stone St
City State Zip Phone
2 New York NY 10012 (212) 529-5945
3 Flushing NY 11367 (718) 261-0196
9 Brooklyn NY 11215 (718) 499-4994
11 New York NY 10019 (212) 719-2819
12 Brooklyn NY 11222 (347) 987-3747
14 New York NY 10023 (212) 362-7260
17 New York NY 10029 (212) 722-6400
25 New York NY 10028 (212) 988-9780
26 Brooklyn NY 11218 (718) 435-0921
32 Oakland Gardens NY 11364 (718) 468-1019
37 Brooklyn NY 11234 (718) 209-0636
38 New York NY 10065 (212) 702-9898
40 Fresh Meadows NY 11366 (718) 591-7901
44 Forest Hills NY 11375 (718) 997-7111
48 Brooklyn NY 11201 (718) 246-2000
59 New York NY 10013 (212) 226-7900
64 New York NY 10029 (212) 987-5414
67 Brooklyn NY 11235 (718) 332-8494
71 Astoria NY 11105 (718) 721-3532
74 New York NY 10075 (646) 590-1925
77 Brooklyn NY 11214 (718) 621-3300
78 New York NY 10024 (212) 873-8500
80 New York NY 10021 (212) 535-9600
81 New York NY 10011 (646) 486-7400
82 Brooklyn NY 11225 (718) 363-8300
89 New York NY 10014 (212) 242-1999
91 New York NY 10009 (212) 388-9811
95 New York NY 10016 (212) 779-1400
98 New York NY 10004 (212) 248-3838
pizza_modern %>% filter(Price >= 3)
# A tibble: 29 x 7 Name Price Address City State Zip Phone <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> 1 La Lanterna C~ 3 129 MacDougal St New York NY 10012 (212) 529~ 2 L Bella Pizze~ 3 69-26 Main St Flushing NY 11367 (718) 261~ 3 La Bruschetta 3 256 7th Ave Brooklyn NY 11215 (718) 499~ 4 Bella Napoli 3 150 W 49th St New York NY 10019 (212) 719~ 5 Paulie Gee's 3 60 Greenpoint A~ Brooklyn NY 11222 (347) 987~ 6 Amsterdam Ale~ 3 340 Amsterdam A~ New York NY 10023 (212) 362~ 7 Armonie 3 100 E 117th St New York NY 10029 (212) 722~ 8 TiramiSu 3 1410 3rd Ave New York NY 10028 (212) 988~ 9 Le Paddock 3 1235 Prospect A~ Brooklyn NY 11218 (718) 435~ 10 Vito's Pizza 3 78-37 Springfie~ Oakland Ga~ NY 11364 (718) 468~ # ... with 19 more rows
pizza_base[pizza_base$Price >= 3 & pizza_base$City == 'New York', ]
Name Price Address City State Zip
2 La Lanterna Caffe 3 129 MacDougal St New York NY 10012
11 Bella Napoli 3 150 W 49th St New York NY 10019
14 Amsterdam Ale House 3 340 Amsterdam Ave New York NY 10023
17 Armonie 3 100 E 117th St New York NY 10029
25 TiramiSu 3 1410 3rd Ave New York NY 10028
38 Serafina Always 4 33 E 61st St New York NY 10065
59 Saluggi's 3 325 Church St New York NY 10013
64 Dreamer's Pizza 3 1850 3rd Ave New York NY 10029
74 Salvo's Pizza Bar 3 1477 York Ave New York NY 10075
78 Osteria Cotta 3 513 Columbus Ave New York NY 10024
80 Mezzaluna 4 1295 3rd Ave New York NY 10021
81 Patsy's Pizzeria 3 318 W 23rd St New York NY 10011
89 Rafele 4 29 7th Ave S New York NY 10014
91 Eleven B 3 174 Ave B New York NY 10009
95 Squisito 3 64 E 34th St New York NY 10016
98 Adrienne's Pizza Bar 3 54 Stone St New York NY 10004
Phone
2 (212) 529-5945
11 (212) 719-2819
14 (212) 362-7260
17 (212) 722-6400
25 (212) 988-9780
38 (212) 702-9898
59 (212) 226-7900
64 (212) 987-5414
74 (646) 590-1925
78 (212) 873-8500
80 (212) 535-9600
81 (646) 486-7400
89 (212) 242-1999
91 (212) 388-9811
95 (212) 779-1400
98 (212) 248-3838
pizza_modern %>% filter(Price >= 3 & City == 'New York')
# A tibble: 16 x 7 Name Price Address City State Zip Phone <chr> <dbl> <chr> <chr> <chr> <dbl> <chr> 1 La Lanterna Caffe 3 129 MacDougal ~ New Yo~ NY 10012 (212) 529-5~ 2 Bella Napoli 3 150 W 49th St New Yo~ NY 10019 (212) 719-2~ 3 Amsterdam Ale Ho~ 3 340 Amsterdam ~ New Yo~ NY 10023 (212) 362-7~ 4 Armonie 3 100 E 117th St New Yo~ NY 10029 (212) 722-6~ 5 TiramiSu 3 1410 3rd Ave New Yo~ NY 10028 (212) 988-9~ 6 Serafina Always 4 33 E 61st St New Yo~ NY 10065 (212) 702-9~ 7 Saluggi's 3 325 Church St New Yo~ NY 10013 (212) 226-7~ 8 Dreamer's Pizza 3 1850 3rd Ave New Yo~ NY 10029 (212) 987-5~ 9 Salvo's Pizza Bar 3 1477 York Ave New Yo~ NY 10075 (646) 590-1~ 10 Osteria Cotta 3 513 Columbus A~ New Yo~ NY 10024 (212) 873-8~ 11 Mezzaluna 4 1295 3rd Ave New Yo~ NY 10021 (212) 535-9~ 12 Patsy's Pizzeria 3 318 W 23rd St New Yo~ NY 10011 (646) 486-7~ 13 Rafele 4 29 7th Ave S New Yo~ NY 10014 (212) 242-1~ 14 Eleven B 3 174 Ave B New Yo~ NY 10009 (212) 388-9~ 15 Squisito 3 64 E 34th St New Yo~ NY 10016 (212) 779-1~ 16 Adrienne's Pizza~ 3 54 Stone St New Yo~ NY 10004 (212) 248-3~
Rows and Columns
pizza_base[pizza_base$Price >= 3, c('Name', 'Price')]
Name Price 2 La Lanterna Caffe 3 3 L Bella Pizzeria 3 9 La Bruschetta 3 11 Bella Napoli 3 12 Paulie Gee's 3 14 Amsterdam Ale House 3 17 Armonie 3 25 TiramiSu 3 26 Le Paddock 3 32 Vito's Pizza 3 37 Dagan Kosher Pizza 3 38 Serafina Always 4 40 Don Giuseppe Trattoria / Ray Bari Pizza 3 44 Due Fratelli 3 48 Monty Q's 3 59 Saluggi's 3 64 Dreamer's Pizza 3 67 Il Fornetto 4 71 Trattoria L'Incontro 4 74 Salvo's Pizza Bar 3 77 Tato's Pizza 3 78 Osteria Cotta 3 80 Mezzaluna 4 81 Patsy's Pizzeria 3 82 Tony's Pizza 3 89 Rafele 4 91 Eleven B 3 95 Squisito 3 98 Adrienne's Pizza Bar 3
pizza_modern %>% filter(Price >= 3) %>% select(Name, Price)
# A tibble: 29 x 2 Name Price <chr> <dbl> 1 La Lanterna Caffe 3 2 L Bella Pizzeria 3 3 La Bruschetta 3 4 Bella Napoli 3 5 Paulie Gee's 3 6 Amsterdam Ale House 3 7 Armonie 3 8 TiramiSu 3 9 Le Paddock 3 10 Vito's Pizza 3 # ... with 19 more rows
Plotting
cols <- c('red', 'green', 'blue', 'yellow', 'black')
plot(price ~ carat, data=diamonds, col=cols[diamonds$cut])
legend('topright', legend=levels(diamonds$cut), col=cols, pch=1)
ggplot(diamonds, aes(x=carat, y=price, color=cut)) + geom_point(shape=1)
boxplot(price ~ cut, data=diamonds)
ggplot(diamonds, aes(x=cut, y=price)) +
geom_jitter(alpha=1/5, size=1, shape=1, width=0.35, aes(color=carat)) +
geom_violin(alpha=1/2, color='grey50', draw_quantiles=c(.25, .5, .75)) +
scale_color_gradient('Diamond Size', low='#56B1F7', high='#c91010') +
scale_y_continuous(labels=scales::dollar) +
labs(x='Cut', y='Price', title='Price vs Cut and Carat')
hist(diamonds$price)
ggplot(diamonds, aes(x=price)) + geom_histogram(bins=30)
hchist(diamonds$price)
plot(econ_ts)
autoplot(econ_ts)
Aggregation
aggregate(price ~ cut, data=diamonds, mean)
cut price 1 Fair 4358.758 2 Good 3928.864 3 Very Good 3981.760 4 Premium 4584.258 5 Ideal 3457.542
diamonds %>% group_by(cut) %>% summarize(AvgPrice=mean(price))
# A tibble: 5 x 2 cut AvgPrice <ord> <dbl> 1 Fair 4359. 2 Good 3929. 3 Very Good 3982. 4 Premium 4584. 5 Ideal 3458.
aggregate(cbind(price, carat) ~ cut, data=diamonds, mean)
cut price carat 1 Fair 4358.758 1.0461366 2 Good 3928.864 0.8491847 3 Very Good 3981.760 0.8063814 4 Premium 4584.258 0.8919549 5 Ideal 3457.542 0.7028370
diamonds %>% group_by(cut) %>% summarize(AvgPrice=mean(price), AvgCarat=mean(carat))
# A tibble: 5 x 3 cut AvgPrice AvgCarat <ord> <dbl> <dbl> 1 Fair 4359. 1.05 2 Good 3929. 0.849 3 Very Good 3982. 0.806 4 Premium 4584. 0.892 5 Ideal 3458. 0.703
Modeling
Became Machine Learning
Became AI
price_formula <- log(price) ~ scale(carat) + scale(x) + scale(y) + scale(z) + cut
price_recipe <- recipe(price ~ carat + x + y + z + cut, data=diamonds) %>% step_center(all_numeric(), -all_outcomes()) %>% step_scale(all_numeric(), -all_outcomes()) %>% step_dummy(cut) %>% step_intercept() %>% step_log(all_outcomes()) %>% prep() price_train <- price_recipe %>% juice()
mod_base <- lm(price_formula, data=diamonds)
mod_tidy <- linear_reg() %>%
set_engine('lm') %>%
fit(price ~ ., data=price_train)
mod_tidy_2 <- linear_reg() %>%
set_engine('stan') %>%
fit(price ~ ., data=price_train)
mod_tidy_3 <- linear_reg() %>%
set_engine('spark') %>%
fit(price ~ ., data=price_train)
mod_tidy_4 <- linear_reg() %>%
set_engine('glmnet') %>%
fit(price ~ ., data=price_train)
mod_tidy_5 <- linear_reg() %>%
set_engine('keras') %>%
fit(price ~ ., data=price_train)
Neural Nets
nnet_base <- nnet::nnet(price_formula, diamonds, size=64)
net <- keras_model_sequential() %>%
layer_dense(units=64, activation='relu', input_shape=ncol(price_train)-1) %>%
layer_dense(units=1, activation='linear')
net %>%
compile(optimizer = "rmsprop",
loss = "mean_squared_error",
metrics = c("mean_absolute_error")
)
net %>%
fit(
x=juice(price_recipe, all_predictors(), composition='matrix'),
y=juice(price_recipe, all_outcomes(), composition='matrix'),
epochs=10, batch_size=64
)
Getting Help
Once you appreciate that you have seriously misread the page, things will become a lot clearer.
Hopefully this one isn’t in the manual or I am about to get shot :-S
10 Years
The Future
Thank You