forked from AdamWilsonLabEDU/SpatialDataScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_DataWrangling.html
More file actions
1143 lines (1064 loc) · 44 KB
/
03_DataWrangling.html
File metadata and controls
1143 lines (1064 loc) · 44 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<title>Data Wrangling 1</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.0.13/css/fa-svg-with-js.css" rel="stylesheet" />
<script src="site_libs/font-awesome-5.0.13/js/fontawesome-all.min.js"></script>
<script src="site_libs/font-awesome-5.0.13/js/fa-v4-shims.min.js"></script>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">GEO 503: Spatial Data Science</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li>
<a href="Syllabus.html">Syllabus</a>
</li>
<li>
<a href="Schedule.html">Schedule</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Content
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="CourseContent.html">About Course Content</a>
</li>
<li class="dropdown-header">Module 1: Introduction to R</li>
<li>
<a href="00_CourseIntroductionFrame.html">00 Course Introduction</a>
</li>
<li>
<a href="01_Rintro.html">01 First Steps</a>
</li>
<li>
<a href="02_graphics.html">02 Graphics</a>
</li>
<li>
<a href="03_DataWrangling.html">03 Data Wrangling</a>
</li>
<li>
<a href="03b_DataWrangling.html">03 Data Wrangling 2</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Module 2: Spatial Analyses</li>
<li>
<a href="04_Spatial_with_sf.html">04 Spatial Data with sf</a>
</li>
<li>
<a href="05_Raster.html">05 Spatial Raster Data</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Module 3: Advanced Programming</li>
<li>
<a href="06_CreatingWorkflows.html">06 Creating Workflows</a>
</li>
<li>
<a href="07_Reproducibile.html">07 Reproducible Research</a>
</li>
<li>
<a href="08_WeatherData.html">08 Weather Data Processing</a>
</li>
<li>
<a href="09_RemoteSensing_appeears.html">09 Satellite Data Processing</a>
</li>
<li>
<a href="11_ParallelProcessing.html">11 Parallel Processing</a>
</li>
<li>
<a href="12_DynamicVisualization.html">12 Dynamic Visualization</a>
</li>
<li>
<a href="13_SDM_Exercise.html">13 Species Distribution Modeling</a>
</li>
<li class="divider"></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Assignments
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Tasklist.html">Task list</a>
</li>
<li>
<a href="DataCamp.html">DataCamp</a>
</li>
<li>
<a href="PackageIntro.html">Package Introduction</a>
</li>
<li>
<a href="Project.html">Final Project</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Resources
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Provenance.html">Provenance</a>
</li>
<li>
<a href="Projects_2017.html">2017 Final Projects</a>
</li>
<li>
<a href="Resources.html">Resources</a>
</li>
<li>
<a href="GitSSHNotes.html">Setting up Github</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/adammwilson/RDataScience">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Data Wrangling 1</h1>
</div>
<div>
<object data="presentations/03_DataWrangling.pdf" type="application/pdf" width="100%" height="600px">
<p>
It appears you don’t have a PDF plugin for this browser. No biggie… you can <a href="presentations/03_DataWrangling.pdf">click here to download the PDF file.</a>
</p>
</object>
</div>
<p>
<a href="presentations/03_DataWrangling.pdf">Download the PDF of the presentation</a>
</p>
<p><a href="scripts/03_DataWrangling.R"><i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> The R Script associated with this page is available here</a>. Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.</p>
<div id="rstudio-shortcuts" class="section level1">
<h1>RStudio Shortcuts</h1>
<div id="running-code" class="section level2">
<h2>Running code</h2>
<ul>
<li><code>ctrl-R</code> (or <code>command-R</code>) to run current line</li>
<li>Highlight <code>code</code> in script and run <code>ctrl-R</code> (or <code>command-R</code>) to run selection</li>
<li>Buttons: <img src="03_assets/Source.png" style="width: 25%"/></li>
</ul>
</div>
<div id="switching-windows" class="section level2">
<h2>Switching windows</h2>
<ul>
<li><code>ctrl-1</code>: script window</li>
<li><code>ctrl-2</code>: console window</li>
</ul>
<blockquote>
<p>Try to run today’s script without using your mouse/trackpad</p>
</blockquote>
</div>
</div>
<div id="data-wrangling" class="section level1">
<h1>Data wrangling</h1>
<div id="useful-packages-tidyverse" class="section level2">
<h2>Useful packages: <a href="https://www.tidyverse.org/packages/"><code>tidyverse</code></a></h2>
<p><a href="https://www.rstudio.com/resources/cheatsheets/">Cheat sheets on website</a> for <a href="https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf">Data Wrangling</a></p>
<pre class="r"><code>library(tidyverse)</code></pre>
<p>Remember use <code>install.packages("tidyverse")</code> to install a new package.</p>
<div id="example-operations-from-here" class="section level3">
<h3>Example operations from <a href="https://cran.rstudio.com/web/packages/dplyr/vignettes/introduction.html">here</a></h3>
</div>
</div>
<div id="new-york-city-flights" class="section level2">
<h2>New York City Flights</h2>
<p>Data from <a href="http://www.transtats.bts.gov/DatabaseInfo.asp?DB_ID=120&Link=0">US Bureau of Transportation Statistics</a> (see <code>?nycflights13</code>)</p>
<pre class="r"><code>library(nycflights13)</code></pre>
<p>Check out the <code>flights</code> object</p>
<pre class="r"><code>head(flights)</code></pre>
<pre><code>## # A tibble: 6 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## # ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## # time_hour <dttm></code></pre>
<div id="object-structure" class="section level3">
<h3>Object <em>Structure</em></h3>
<p>Check out data <em>structure</em> with <code>glimpse()</code></p>
<pre class="r"><code>glimpse(flights)</code></pre>
<pre><code>## Observations: 336,776
## Variables: 19
## $ year <int> 2013, 2013, 2013, 2013, 2013, 2013, 2013, 2013,...
## $ month <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ day <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,...
## $ dep_time <int> 517, 533, 542, 544, 554, 554, 555, 557, 557, 55...
## $ sched_dep_time <int> 515, 529, 540, 545, 600, 558, 600, 600, 600, 60...
## $ dep_delay <dbl> 2, 4, 2, -1, -6, -4, -5, -3, -3, -2, -2, -2, -2...
## $ arr_time <int> 830, 850, 923, 1004, 812, 740, 913, 709, 838, 7...
## $ sched_arr_time <int> 819, 830, 850, 1022, 837, 728, 854, 723, 846, 7...
## $ arr_delay <dbl> 11, 20, 33, -18, -25, 12, 19, -14, -8, 8, -2, -...
## $ carrier <chr> "UA", "UA", "AA", "B6", "DL", "UA", "B6", "EV",...
## $ flight <int> 1545, 1714, 1141, 725, 461, 1696, 507, 5708, 79...
## $ tailnum <chr> "N14228", "N24211", "N619AA", "N804JB", "N668DN...
## $ origin <chr> "EWR", "LGA", "JFK", "JFK", "LGA", "EWR", "EWR"...
## $ dest <chr> "IAH", "IAH", "MIA", "BQN", "ATL", "ORD", "FLL"...
## $ air_time <dbl> 227, 227, 160, 183, 116, 150, 158, 53, 140, 138...
## $ distance <dbl> 1400, 1416, 1089, 1576, 762, 719, 1065, 229, 94...
## $ hour <dbl> 5, 5, 5, 5, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5,...
## $ minute <dbl> 15, 29, 40, 45, 0, 58, 0, 0, 0, 0, 0, 0, 0, 0, ...
## $ time_hour <dttm> 2013-01-01 05:00:00, 2013-01-01 05:00:00, 2013...</code></pre>
</div>
</div>
</div>
<div id="dplyr-verbs" class="section level1">
<h1><code>dplyr</code> “verbs”</h1>
<ul>
<li><code>select()</code> and <code>rename()</code>: Extract existing variables</li>
<li><code>filter()</code> and <code>slice()</code>: Extract existing observations</li>
<li><code>arrange()</code></li>
<li><code>distinct()</code></li>
<li><code>mutate()</code> and <code>transmute()</code>: Derive new variables</li>
<li><code>summarise()</code>: Change the unit of analysis</li>
<li><code>sample_n()</code> and <code>sample_frac()</code></li>
</ul>
<div id="useful-select-functions" class="section level2">
<h2>Useful select functions</h2>
<ul>
<li>“<code>-</code>” Select everything but</li>
<li>“<code>:</code>” Select range</li>
<li><code>contains()</code> Select columns whose name contains a character string</li>
<li><code>ends_with()</code> Select columns whose name ends with a string</li>
<li><code>everything()</code> Select every column</li>
<li><code>matches()</code> Select columns whose name matches a regular expression</li>
<li><code>num_range()</code> Select columns named x1, x2, x3, x4, x5</li>
<li><code>one_of()</code> Select columns whose names are in a group of names</li>
<li><code>starts_with()</code> Select columns whose name starts with a character string</li>
</ul>
<div id="select-examples" class="section level3">
<h3><code>select()</code> examples</h3>
<p>Select only the <code>year</code>, <code>month</code>, and <code>day</code> columns:</p>
<pre class="r"><code>select(flights,year, month, day)</code></pre>
<pre><code>## # A tibble: 336,776 x 3
## year month day
## <int> <int> <int>
## 1 2013 1 1
## 2 2013 1 1
## 3 2013 1 1
## 4 2013 1 1
## 5 2013 1 1
## 6 2013 1 1
## 7 2013 1 1
## 8 2013 1 1
## 9 2013 1 1
## 10 2013 1 1
## # ... with 336,766 more rows</code></pre>
</div>
<div id="select-examples-1" class="section level3">
<h3><code>select()</code> examples</h3>
<p>Select everything <em>except</em> the <code>tailnum</code>:</p>
<pre class="r"><code>select(flights,-tailnum)</code></pre>
<pre><code>## # A tibble: 336,776 x 18
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ... with 336,766 more rows, and 11 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, origin <chr>,
## # dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## # time_hour <dttm></code></pre>
<p>Select all columns containing the string <code>"time"</code>:</p>
<pre class="r"><code>select(flights,contains("time"))</code></pre>
<pre><code>## # A tibble: 336,776 x 6
## dep_time sched_dep_time arr_time sched_arr_time air_time
## <int> <int> <int> <int> <dbl>
## 1 517 515 830 819 227
## 2 533 529 850 830 227
## 3 542 540 923 850 160
## 4 544 545 1004 1022 183
## 5 554 600 812 837 116
## 6 554 558 740 728 150
## 7 555 600 913 854 158
## 8 557 600 709 723 53
## 9 557 600 838 846 140
## 10 558 600 753 745 138
## # ... with 336,766 more rows, and 1 more variable: time_hour <dttm></code></pre>
<p>You can also rename columns with <code>select()</code></p>
<pre class="r"><code>select(flights,year,carrier,destination=dest)</code></pre>
<pre><code>## # A tibble: 336,776 x 3
## year carrier destination
## <int> <chr> <chr>
## 1 2013 UA IAH
## 2 2013 UA IAH
## 3 2013 AA MIA
## 4 2013 B6 BQN
## 5 2013 DL ATL
## 6 2013 UA ORD
## 7 2013 B6 FLL
## 8 2013 EV IAD
## 9 2013 B6 MCO
## 10 2013 AA ORD
## # ... with 336,766 more rows</code></pre>
</div>
</div>
<div id="filter-observations" class="section level2">
<h2><code>filter()</code> observations</h2>
<p>Filter all flights that departed on on January 1st:</p>
<pre class="r"><code>filter(flights, month == 1, day == 1)</code></pre>
<pre><code>## # A tibble: 842 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ... with 832 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
</div>
<div id="base-r-method" class="section level2">
<h2><em>Base</em> R method</h2>
<p>This is equivalent to the more verbose code in base R:</p>
<pre class="r"><code>flights[flights$month == 1 & flights$day == 1, ]</code></pre>
<pre><code>## # A tibble: 842 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ... with 832 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
<p>Compare with <code>dplyr</code> method:</p>
<pre class="r"><code>filter(flights, month == 1, day == 1)</code></pre>
<div class="well">
<p>Filter the <code>flights</code> data set to keep only evening flights (<code>dep_time</code> after 1600) in June.</p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo1">
Show Solution
</button>
<div id="demo1" class="collapse">
<pre class="r"><code>filter(flights,dep_time>1600,month==6)</code></pre>
<pre><code>## # A tibble: 10,117 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 6 1 1602 1505 57 1721
## 2 2013 6 1 1602 1605 -3 1824
## 3 2013 6 1 1602 1610 -8 1748
## 4 2013 6 1 1603 1610 -7 1839
## 5 2013 6 1 1603 1545 18 1726
## 6 2013 6 1 1605 1608 -3 1742
## 7 2013 6 1 1605 1600 5 1801
## 8 2013 6 1 1605 1614 -9 1801
## 9 2013 6 1 1608 1600 8 1807
## 10 2013 6 1 1609 1615 -6 1817
## # ... with 10,107 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
</div>
</div>
</div>
<div id="other-boolean-expressions" class="section level2">
<h2>Other <em>boolean</em> expressions</h2>
<p><code>filter()</code> is similar to <code>subset()</code> except it handles any number of filtering conditions joined together with <code>&</code>.</p>
<p>You can also use other boolean operators, such as <em>OR</em> (“|”):</p>
<pre class="r"><code>filter(flights, month == 1 | month == 2)</code></pre>
<pre><code>## # A tibble: 51,955 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ... with 51,945 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
<div class="well">
<p>Filter the <code>flights</code> data set to keep only flights where the <code>distance</code> is greater than 1000 OR the <code>air_time</code> is more than 100</p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo2">
Show Solution
</button>
<div id="demo2" class="collapse">
<p><br></p>
<pre class="r"><code>filter(flights,distance>1000|air_time>100)</code></pre>
<pre><code>## # A tibble: 222,479 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 838
## 9 2013 1 1 558 600 -2 753
## 10 2013 1 1 558 600 -2 849
## # ... with 222,469 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
</div>
</div>
</div>
<div id="select-rows-with-slice" class="section level2">
<h2>Select rows with <code>slice()</code>:</h2>
<pre class="r"><code>slice(flights, 1:10)</code></pre>
<pre><code>## # A tibble: 10 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ... with 12 more variables: sched_arr_time <int>, arr_delay <dbl>,
## # carrier <chr>, flight <int>, tailnum <chr>, origin <chr>, dest <chr>,
## # air_time <dbl>, distance <dbl>, hour <dbl>, minute <dbl>,
## # time_hour <dttm></code></pre>
</div>
<div id="arrange-rows" class="section level2">
<h2><code>arrange()</code> rows</h2>
<p><code>arrange()</code> is similar to <code>filter()</code> except it reorders instead of filtering.</p>
<pre class="r"><code>arrange(flights, year, month, day)</code></pre>
<pre><code>## # A tibble: 336,776 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 1 517 515 2 830
## 2 2013 1 1 533 529 4 850
## 3 2013 1 1 542 540 2 923
## 4 2013 1 1 544 545 -1 1004
## 5 2013 1 1 554 600 -6 812
## 6 2013 1 1 554 558 -4 740
## 7 2013 1 1 555 600 -5 913
## 8 2013 1 1 557 600 -3 709
## 9 2013 1 1 557 600 -3 838
## 10 2013 1 1 558 600 -2 753
## # ... with 336,766 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
<p><em>Base</em> R method:</p>
<pre class="r"><code>flights[order(flights$year, flights$month, flights$day), ]</code></pre>
</div>
<div id="descending-order-desc" class="section level2">
<h2>Descending order: <code>desc()</code></h2>
<pre class="r"><code>arrange(flights, desc(arr_delay))</code></pre>
<pre><code>## # A tibble: 336,776 x 19
## year month day dep_time sched_dep_time dep_delay arr_time
## <int> <int> <int> <int> <int> <dbl> <int>
## 1 2013 1 9 641 900 1301 1242
## 2 2013 6 15 1432 1935 1137 1607
## 3 2013 1 10 1121 1635 1126 1239
## 4 2013 9 20 1139 1845 1014 1457
## 5 2013 7 22 845 1600 1005 1044
## 6 2013 4 10 1100 1900 960 1342
## 7 2013 3 17 2321 810 911 135
## 8 2013 7 22 2257 759 898 121
## 9 2013 12 5 756 1700 896 1058
## 10 2013 5 3 1133 2055 878 1250
## # ... with 336,766 more rows, and 12 more variables: sched_arr_time <int>,
## # arr_delay <dbl>, carrier <chr>, flight <int>, tailnum <chr>,
## # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
## # minute <dbl>, time_hour <dttm></code></pre>
<p><em>Base</em> R method:</p>
<pre class="r"><code>flights[order(desc(flights$arr_delay)), ]</code></pre>
</div>
<div id="distinct-find-distinct-rows" class="section level2">
<h2>Distinct: Find distinct rows</h2>
<pre class="r"><code>distinct(
select(flights,carrier)
)</code></pre>
<pre><code>## # A tibble: 16 x 1
## carrier
## <chr>
## 1 UA
## 2 AA
## 3 B6
## 4 DL
## 5 EV
## 6 MQ
## 7 US
## 8 WN
## 9 VX
## 10 FL
## 11 AS
## 12 9E
## 13 F9
## 14 HA
## 15 YV
## 16 OO</code></pre>
</div>
<div id="mutate-derive-new-variables" class="section level2">
<h2>Mutate: Derive new variables</h2>
<p>Adds columns with calculations based on other columns.</p>
<p>Average air speed (miles/hour):</p>
<pre class="r"><code>mutate(flights,ave_speed=distance/(air_time/60))%>%
select(distance, air_time,ave_speed)</code></pre>
<pre><code>## # A tibble: 336,776 x 3
## distance air_time ave_speed
## <dbl> <dbl> <dbl>
## 1 1400 227 370.
## 2 1416 227 374.
## 3 1089 160 408.
## 4 1576 183 517.
## 5 762 116 394.
## 6 719 150 288.
## 7 1065 158 404.
## 8 229 53 259.
## 9 944 140 405.
## 10 733 138 319.
## # ... with 336,766 more rows</code></pre>
</div>
<div id="chaining-operations" class="section level2">
<h2>Chaining Operations</h2>
<p>Learn to performing multiple operations sequentially with a <em>pipe</em> character (<code>%>%</code>)</p>
<ol style="list-style-type: decimal">
<li>Group by a variable</li>
<li>Select some columns</li>
<li>Summarize observations</li>
<li>Filter by results</li>
</ol>
<p>With temporary objects:</p>
<pre class="r"><code>a1 <- group_by(flights, year, month, day)
a2 <- select(a1, arr_delay, dep_delay)</code></pre>
<pre><code>## Adding missing grouping variables: `year`, `month`, `day`</code></pre>
<pre class="r"><code>a3 <- summarise(a2,
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE))
a4 <- filter(a3, arr > 30 | dep > 30)
head(a4)</code></pre>
<pre><code>## # A tibble: 6 x 5
## # Groups: year, month [3]
## year month day arr dep
## <int> <int> <int> <dbl> <dbl>
## 1 2013 1 16 34.2 24.6
## 2 2013 1 31 32.6 28.7
## 3 2013 2 11 36.3 39.1
## 4 2013 2 27 31.3 37.8
## 5 2013 3 8 85.9 83.5
## 6 2013 3 18 41.3 30.1</code></pre>
<p>If you don’t want to save the intermediate results: wrap the function calls inside each other:</p>
<pre class="r"><code>filter(
summarise(
select(
group_by(flights, year, month, day),
arr_delay, dep_delay
),
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE)
),
arr > 30 | dep > 30
)</code></pre>
<pre><code>## Adding missing grouping variables: `year`, `month`, `day`</code></pre>
<pre><code>## # A tibble: 49 x 5
## # Groups: year, month [11]
## year month day arr dep
## <int> <int> <int> <dbl> <dbl>
## 1 2013 1 16 34.2 24.6
## 2 2013 1 31 32.6 28.7
## 3 2013 2 11 36.3 39.1
## 4 2013 2 27 31.3 37.8
## 5 2013 3 8 85.9 83.5
## 6 2013 3 18 41.3 30.1
## 7 2013 4 10 38.4 33.0
## 8 2013 4 12 36.0 34.8
## 9 2013 4 18 36.0 34.9
## 10 2013 4 19 47.9 46.1
## 11 2013 4 22 37.8 30.6
## 12 2013 4 25 33.7 23.3
## 13 2013 5 8 39.6 43.2
## 14 2013 5 23 62.0 51.1
## 15 2013 5 24 24.3 30.3
## 16 2013 6 2 26.1 34.0
## 17 2013 6 10 28.0 30.6
## 18 2013 6 13 63.8 45.8
## 19 2013 6 18 37.6 36.0
## 20 2013 6 24 51.2 47.2
## 21 2013 6 25 41.5 43.1
## 22 2013 6 26 27.3 30.6
## 23 2013 6 27 44.8 40.9
## 24 2013 6 28 45.0 48.8
## 25 2013 6 30 43.5 44.2
## 26 2013 7 1 58.3 56.2
## 27 2013 7 7 40.3 36.6
## 28 2013 7 8 29.6 37.3
## 29 2013 7 9 31.3 30.7
## 30 2013 7 10 59.6 52.9
## 31 2013 7 22 62.8 46.7
## 32 2013 7 23 45.0 44.7
## 33 2013 7 28 49.8 37.7
## 34 2013 8 1 36.0 34.6
## 35 2013 8 8 55.5 43.3
## 36 2013 8 9 43.3 34.7
## 37 2013 8 22 30.0 33.6
## 38 2013 8 28 35.2 40.5
## 39 2013 9 2 45.5 53.0
## 40 2013 9 12 58.9 50.0
## 41 2013 10 7 39.0 39.1
## 42 2013 10 11 18.9 31.2
## 43 2013 12 5 51.7 52.3
## 44 2013 12 8 36.9 21.5
## 45 2013 12 9 42.6 34.8
## 46 2013 12 10 44.5 26.5
## 47 2013 12 14 46.4 28.4
## 48 2013 12 17 55.9 40.7
## 49 2013 12 23 32.2 32.3</code></pre>
<p>Arguments are distant from function -> difficult to read!</p>
</div>
<div id="chaining-operations-1" class="section level2">
<h2>Chaining Operations</h2>
<p><code>%>%</code> (from the dplyr package) allows you to <em>pipe</em> together various commands.</p>
<p><code>x %>% f(y)</code> turns into <code>f(x, y)</code></p>
<p>So you can use it to rewrite multiple operations that you can read left-to-right, top-to-bottom:</p>
<pre class="r"><code>flights %>%
group_by(year, month, day) %>%
select(arr_delay, dep_delay) %>%
summarise(
arr = mean(arr_delay, na.rm = TRUE),
dep = mean(dep_delay, na.rm = TRUE)
) %>%
filter(arr > 30 | dep > 30)</code></pre>
<pre><code>## Adding missing grouping variables: `year`, `month`, `day`</code></pre>
<pre><code>## # A tibble: 49 x 5
## # Groups: year, month [11]
## year month day arr dep
## <int> <int> <int> <dbl> <dbl>
## 1 2013 1 16 34.2 24.6
## 2 2013 1 31 32.6 28.7
## 3 2013 2 11 36.3 39.1
## 4 2013 2 27 31.3 37.8
## 5 2013 3 8 85.9 83.5
## 6 2013 3 18 41.3 30.1
## 7 2013 4 10 38.4 33.0
## 8 2013 4 12 36.0 34.8
## 9 2013 4 18 36.0 34.9
## 10 2013 4 19 47.9 46.1
## 11 2013 4 22 37.8 30.6
## 12 2013 4 25 33.7 23.3
## 13 2013 5 8 39.6 43.2
## 14 2013 5 23 62.0 51.1
## 15 2013 5 24 24.3 30.3
## 16 2013 6 2 26.1 34.0
## 17 2013 6 10 28.0 30.6
## 18 2013 6 13 63.8 45.8
## 19 2013 6 18 37.6 36.0
## 20 2013 6 24 51.2 47.2
## 21 2013 6 25 41.5 43.1
## 22 2013 6 26 27.3 30.6
## 23 2013 6 27 44.8 40.9
## 24 2013 6 28 45.0 48.8
## 25 2013 6 30 43.5 44.2
## 26 2013 7 1 58.3 56.2
## 27 2013 7 7 40.3 36.6
## 28 2013 7 8 29.6 37.3
## 29 2013 7 9 31.3 30.7
## 30 2013 7 10 59.6 52.9
## 31 2013 7 22 62.8 46.7
## 32 2013 7 23 45.0 44.7
## 33 2013 7 28 49.8 37.7
## 34 2013 8 1 36.0 34.6
## 35 2013 8 8 55.5 43.3
## 36 2013 8 9 43.3 34.7
## 37 2013 8 22 30.0 33.6
## 38 2013 8 28 35.2 40.5
## 39 2013 9 2 45.5 53.0
## 40 2013 9 12 58.9 50.0
## 41 2013 10 7 39.0 39.1
## 42 2013 10 11 18.9 31.2
## 43 2013 12 5 51.7 52.3
## 44 2013 12 8 36.9 21.5
## 45 2013 12 9 42.6 34.8