forked from sta523-fa20/lecture_slides
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lec_06.html
1526 lines (1211 loc) · 45.1 KB
/
lec_06.html
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 lang="" xml:lang="">
<head>
<title>Data manipulation with dplyr</title>
<meta charset="utf-8" />
<meta name="author" content="Shawn Santo" />
<link rel="stylesheet" href="slides.css" type="text/css" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# Data manipulation with dplyr
## Programming for Statistical Science
### Shawn Santo
---
## Supplementary materials
Full video lecture available in Zoom Cloud Recordings
Additional resources
- `dplyr` [cheat sheet](https://github.com/rstudio/cheatsheets/raw/master/data-transformation.pdf)
- [dplyr vignette](https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html)
- [Chapter 5](https://r4ds.had.co.nz/transform.html), R for Data Science
---
## Getting started
```r
library(tidyverse)
```
```r
── Attaching packages ──────────────── tidyverse 1.3.0 ──
✓ ggplot2 3.3.2 ✓ purrr 0.3.4
✓ tibble 3.0.3 ✓ dplyr 1.0.0
✓ tidyr 1.1.0 ✓ stringr 1.4.0
✓ readr 1.3.1 ✓ forcats 0.5.0
── Conflicts ────────────────────── tidyverse_conflicts() ──
x dplyr::filter() masks stats::filter()
x dplyr::lag() masks stats::lag()
```
Also, load `nycflights13`.
```r
library(nycflights13)
```
---
class: inverse, center, middle
# Pipes
---
## Pipes in R
Infix function `%>%` is a forward-pipe operator. It allows you to pipe an object
forward into a function or call expression.
You can think about the following sequence of actions -
find keys, unlock car, start car, drive to school, park.
<br/>
Expressed as a set of nested functions in R pseudo code this would look like:
```r
park(drive(start_car(unlock_car(find("keys"))), to = "campus"))
```
<br/>
Writing it out using pipes give it a more natural (and easier to read)
structure:
```r
find("keys") %>%
unlock_car() %>%
start_car() %>%
drive(to = "campus") %>%
park()
```
---
## Approaches
All of the following are fine, it comes down to personal preference.
Nested:
```r
h(g(f(x), y = 1), z = 1)
```
Piped:
```r
f(x) %>%
g(y = 1) %>%
h(z = 1)
```
Intermediate:
```r
res <- f(x)
res <- g(res, y = 1)
res <- h(res, z = 1)
```
---
## What about other arguments?
By default, the object on the left-hand side of ` %>% ` is placed as the value
to the first argument in the function on the right-hand side of ` %>% `.
To pass the value to other arguments a `.` is used. For example,
--
```r
data.frame(a = 1:3, b = 3:1) %>% lm(a ~ b, data = .)
```
```
#>
#> Call:
#> lm(formula = a ~ b, data = .)
#>
#> Coefficients:
#> (Intercept) b
#> 4 -1
```
--
```r
data.frame(a = 1:3, b = 3:1) %>% .[[1]]
```
```
#> [1] 1 2 3
```
--
```r
data.frame(a = 1:3, b = 3:1) %>% .[[length(.)]]
```
```
#> [1] 3 2 1
```
---
class: inverse, center, middle
# Data wrangling with `dplyr`
---
## A grammar of data manipulation
Package `dplyr` is based on the concepts of functions as verbs
that manipulate data frames.
Single data frame functions / verbs:
.small-text[
| Function | Description |
|--------------------------------|--------------------------------------|
| `filter()` | pick rows matching criteria |
| `slice()` | pick rows using indices |
| `select()` | pick columns by name |
| `pull()` | grab a column as a vector |
| `rename()` | rename specific columns |
| `arrange()` | reorder rows |
| `mutate()` | add new variables |
| `transmute()` | create new data frame with variables |
| `distinct()` | filter for unique rows |
| `sample_n()` / `sample_frac()` | randomly sample rows |
| `summarise()` | reduce variables to values |
]
... many more.
---
## `dplyr` rules
1. First argument is *always* a data frame
2. Subsequent arguments say what to do with that data frame
3. Almost always returns a data frame
4. Doesn't modify in place
<br/><br/>
Based on rules 1 and 3, it is natural to apply ` %>% ` in a sequence
of `dplyr` functions for data wrangling purposes.
---
## Data
We will demonstrate `dplyr`'s functionality using the `nycflights13` package.
.small[
```r
flights
```
```
#> # A tibble: 336,776 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 517 515 2 830 819
#> 2 2013 1 1 533 529 4 850 830
#> 3 2013 1 1 542 540 2 923 850
#> 4 2013 1 1 544 545 -1 1004 1022
#> 5 2013 1 1 554 600 -6 812 837
#> 6 2013 1 1 554 558 -4 740 728
#> 7 2013 1 1 555 600 -5 913 854
#> 8 2013 1 1 557 600 -3 709 723
#> 9 2013 1 1 557 600 -3 838 846
#> 10 2013 1 1 558 600 -2 753 745
#> # … with 336,766 more rows, and 11 more variables: 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>
```
]
---
## `filter()` - March flights
.small[
```r
flights %>%
filter(month == 3)
```
```
#> # A tibble: 28,834 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 3 1 4 2159 125 318 56
#> 2 2013 3 1 50 2358 52 526 438
#> 3 2013 3 1 117 2245 152 223 2354
#> 4 2013 3 1 454 500 -6 633 648
#> 5 2013 3 1 505 515 -10 746 810
#> 6 2013 3 1 521 530 -9 813 827
#> 7 2013 3 1 537 540 -3 856 850
#> 8 2013 3 1 541 545 -4 1014 1023
#> 9 2013 3 1 549 600 -11 639 703
#> 10 2013 3 1 550 600 -10 747 801
#> # … with 28,824 more rows, and 11 more variables: 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>
```
]
---
## `filter()` - flights in the first 7 days of March
.small[
```r
flights %>%
filter(month == 3, day <= 7)
```
```
#> # A tibble: 6,530 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 3 1 4 2159 125 318 56
#> 2 2013 3 1 50 2358 52 526 438
#> 3 2013 3 1 117 2245 152 223 2354
#> 4 2013 3 1 454 500 -6 633 648
#> 5 2013 3 1 505 515 -10 746 810
#> 6 2013 3 1 521 530 -9 813 827
#> 7 2013 3 1 537 540 -3 856 850
#> 8 2013 3 1 541 545 -4 1014 1023
#> 9 2013 3 1 549 600 -11 639 703
#> 10 2013 3 1 550 600 -10 747 801
#> # … with 6,520 more rows, and 11 more variables: 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>
```
]
---
## `filter()` - flights to LAX *or* RDU in March
.small[
```r
flights %>%
filter(dest == "LAX" | dest == "RDU", month == 3)
```
```
#> # A tibble: 1,935 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 3 1 607 610 -3 832 925
#> 2 2013 3 1 608 615 -7 737 750
#> 3 2013 3 1 623 630 -7 753 810
#> 4 2013 3 1 629 632 -3 844 952
#> 5 2013 3 1 657 700 -3 953 1034
#> 6 2013 3 1 714 715 -1 939 1037
#> 7 2013 3 1 716 710 6 958 1035
#> 8 2013 3 1 727 730 -3 1007 1100
#> 9 2013 3 1 803 810 -7 923 955
#> 10 2013 3 1 823 824 -1 954 1014
#> # … with 1,925 more rows, and 11 more variables: 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>
```
]
---
## `slice()` - first 10 flights
.small[
```r
flights %>%
slice(1:10)
```
```
#> # A tibble: 10 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 1 1 517 515 2 830 819
#> 2 2013 1 1 533 529 4 850 830
#> 3 2013 1 1 542 540 2 923 850
#> 4 2013 1 1 544 545 -1 1004 1022
#> 5 2013 1 1 554 600 -6 812 837
#> 6 2013 1 1 554 558 -4 740 728
#> 7 2013 1 1 555 600 -5 913 854
#> 8 2013 1 1 557 600 -3 709 723
#> 9 2013 1 1 557 600 -3 838 846
#> 10 2013 1 1 558 600 -2 753 745
#> # … with 11 more variables: 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>
```
]
---
## `slice()` - last 5 flights
.small[
```r
flights %>%
slice((n() - 4):n())
```
```
#> # A tibble: 5 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 9 30 NA 1455 NA NA 1634
#> 2 2013 9 30 NA 2200 NA NA 2312
#> 3 2013 9 30 NA 1210 NA NA 1330
#> 4 2013 9 30 NA 1159 NA NA 1344
#> 5 2013 9 30 NA 840 NA NA 1020
#> # … with 11 more variables: 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>
```
]
---
## `select()` - specific variables
```r
flights %>%
select(year, month, day)
```
```
#> # 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
```
---
## `select()` - exclude variables
.small[
```r
flights %>%
select(-year, -month, -day)
```
```
#> # A tibble: 336,776 x 16
#> dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
#> <int> <int> <dbl> <int> <int> <dbl> <chr>
#> 1 517 515 2 830 819 11 UA
#> 2 533 529 4 850 830 20 UA
#> 3 542 540 2 923 850 33 AA
#> 4 544 545 -1 1004 1022 -18 B6
#> 5 554 600 -6 812 837 -25 DL
#> 6 554 558 -4 740 728 12 UA
#> 7 555 600 -5 913 854 19 B6
#> 8 557 600 -3 709 723 -14 EV
#> 9 557 600 -3 838 846 -8 B6
#> 10 558 600 -2 753 745 8 AA
#> # … with 336,766 more rows, and 9 more variables: flight <int>, tailnum <chr>,
#> # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
#> # minute <dbl>, time_hour <dttm>
```
]
---
## `select()` - ranges
```r
flights %>%
select(year:day)
```
```
#> # 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
```
---
## `select()` - exclude ranges
.small[
```r
flights %>%
select(-(year:day))
```
```
#> # A tibble: 336,776 x 16
#> dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
#> <int> <int> <dbl> <int> <int> <dbl> <chr>
#> 1 517 515 2 830 819 11 UA
#> 2 533 529 4 850 830 20 UA
#> 3 542 540 2 923 850 33 AA
#> 4 544 545 -1 1004 1022 -18 B6
#> 5 554 600 -6 812 837 -25 DL
#> 6 554 558 -4 740 728 12 UA
#> 7 555 600 -5 913 854 19 B6
#> 8 557 600 -3 709 723 -14 EV
#> 9 557 600 -3 838 846 -8 B6
#> 10 558 600 -2 753 745 8 AA
#> # … with 336,766 more rows, and 9 more variables: flight <int>, tailnum <chr>,
#> # origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>, hour <dbl>,
#> # minute <dbl>, time_hour <dttm>
```
]
---
## `select()` - matching
.small[
```r
flights %>%
select(contains("dep"), contains("arr"))
```
```
#> # A tibble: 336,776 x 7
#> dep_time sched_dep_time dep_delay arr_time sched_arr_time arr_delay carrier
#> <int> <int> <dbl> <int> <int> <dbl> <chr>
#> 1 517 515 2 830 819 11 UA
#> 2 533 529 4 850 830 20 UA
#> 3 542 540 2 923 850 33 AA
#> 4 544 545 -1 1004 1022 -18 B6
#> 5 554 600 -6 812 837 -25 DL
#> 6 554 558 -4 740 728 12 UA
#> 7 555 600 -5 913 854 19 B6
#> 8 557 600 -3 709 723 -14 EV
#> 9 557 600 -3 838 846 -8 B6
#> 10 558 600 -2 753 745 8 AA
#> # … with 336,766 more rows
```
]
---
```r
flights %>%
select(starts_with("dep"), starts_with("arr"))
```
```
#> # A tibble: 336,776 x 4
#> dep_time dep_delay arr_time arr_delay
#> <int> <dbl> <int> <dbl>
#> 1 517 2 830 11
#> 2 533 4 850 20
#> 3 542 2 923 33
#> 4 544 -1 1004 -18
#> 5 554 -6 812 -25
#> 6 554 -4 740 12
#> 7 555 -5 913 19
#> 8 557 -3 709 -14
#> 9 557 -3 838 -8
#> 10 558 -2 753 8
#> # … with 336,766 more rows
```
Other helpers: `ends_with()`, `matches()`, `num_range()`, `one_of()`,
`everything()`, `last_col()`
---
## `select_if()`
```r
flights %>%
select_if(function(x) !is.numeric(x))
```
```
#> # A tibble: 336,776 x 5
#> carrier tailnum origin dest time_hour
#> <chr> <chr> <chr> <chr> <dttm>
#> 1 UA N14228 EWR IAH 2013-01-01 05:00:00
#> 2 UA N24211 LGA IAH 2013-01-01 05:00:00
#> 3 AA N619AA JFK MIA 2013-01-01 05:00:00
#> 4 B6 N804JB JFK BQN 2013-01-01 05:00:00
#> 5 DL N668DN LGA ATL 2013-01-01 06:00:00
#> 6 UA N39463 EWR ORD 2013-01-01 05:00:00
#> 7 B6 N516JB EWR FLL 2013-01-01 06:00:00
#> 8 EV N829AS LGA IAD 2013-01-01 06:00:00
#> 9 B6 N593JB JFK MCO 2013-01-01 06:00:00
#> 10 AA N3ALAA LGA ORD 2013-01-01 06:00:00
#> # … with 336,766 more rows
```
--
Alternatively,
```r
flights %>%
select_if(~!is.numeric(.))
```
---
## `pull()` - grab a vector
```r
names(flights)
```
```
#> [1] "year" "month" "day" "dep_time"
#> [5] "sched_dep_time" "dep_delay" "arr_time" "sched_arr_time"
#> [9] "arr_delay" "carrier" "flight" "tailnum"
#> [13] "origin" "dest" "air_time" "distance"
#> [17] "hour" "minute" "time_hour"
```
```r
flights %>% pull("year") %>% head()
```
```
#> [1] 2013 2013 2013 2013 2013 2013
```
--
```r
flights %>% pull(1) %>% head
```
```
#> [1] 2013 2013 2013 2013 2013 2013
```
--
```r
flights %>% pull(-1) %>% .[1:4]
```
```
#> [1] "2013-01-01 05:00:00 EST" "2013-01-01 05:00:00 EST"
#> [3] "2013-01-01 05:00:00 EST" "2013-01-01 05:00:00 EST"
```
---
## `arrange()` - sort data
.small[
```r
flights %>%
filter(month == 3, day == 2) %>%
arrange(origin, dest)
```
```
#> # A tibble: 765 x 19
#> year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
#> <int> <int> <int> <int> <int> <dbl> <int> <int>
#> 1 2013 3 2 1336 1329 7 1426 1432
#> 2 2013 3 2 628 629 -1 837 849
#> 3 2013 3 2 637 640 -3 903 915
#> 4 2013 3 2 743 745 -2 945 1010
#> 5 2013 3 2 857 900 -3 1117 1126
#> 6 2013 3 2 1027 1030 -3 1234 1247
#> 7 2013 3 2 1134 1145 -11 1332 1359
#> 8 2013 3 2 1412 1415 -3 1636 1630
#> 9 2013 3 2 1633 1636 -3 1848 1908
#> 10 2013 3 2 1655 1700 -5 1857 1924
#> # … with 755 more rows, and 11 more variables: 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>
```
]
---
## `arrange()` & `desc()`
By default, sorting is done in ascending order. To change that, use `desc()`.
```r
flights %>%
filter(month == 3, day == 2) %>%
arrange(desc(origin), dest) %>%
select(origin, dest, tailnum)
```
```
#> # A tibble: 765 x 3
#> origin dest tailnum
#> <chr> <chr> <chr>
#> 1 LGA ATL N928AT
#> 2 LGA ATL N623DL
#> 3 LGA ATL N680DA
#> 4 LGA ATL N996AT
#> 5 LGA ATL N510MQ
#> 6 LGA ATL N663DN
#> 7 LGA ATL N942DL
#> 8 LGA ATL N511MQ
#> 9 LGA ATL N910DE
#> 10 LGA ATL N902DE
#> # … with 755 more rows
```
---
## `mutate()` - modify columns
```r
flights %>%
select(year:day) %>%
mutate(date = paste(year, month, day, sep = "/"))
```
```
#> # A tibble: 336,776 x 4
#> year month day date
#> <int> <int> <int> <chr>
#> 1 2013 1 1 2013/1/1
#> 2 2013 1 1 2013/1/1
#> 3 2013 1 1 2013/1/1
#> 4 2013 1 1 2013/1/1
#> 5 2013 1 1 2013/1/1
#> 6 2013 1 1 2013/1/1
#> 7 2013 1 1 2013/1/1
#> 8 2013 1 1 2013/1/1
#> 9 2013 1 1 2013/1/1
#> 10 2013 1 1 2013/1/1
#> # … with 336,766 more rows
```
---
## `transmute()` - create tibble from existing columns
```r
flights %>%
transmute(date = paste(year, month, day, sep = "/"))
```
```
#> # A tibble: 336,776 x 1
#> date
#> <chr>
#> 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
```
---
## `distinct()` - find unique rows
```r
flights %>%
select(origin, dest) %>%
distinct() %>%
arrange(origin, dest)
```
```
#> # A tibble: 224 x 2
#> origin dest
#> <chr> <chr>
#> 1 EWR ALB
#> 2 EWR ANC
#> 3 EWR ATL
#> 4 EWR AUS
#> 5 EWR AVL
#> 6 EWR BDL
#> 7 EWR BNA
#> 8 EWR BOS
#> 9 EWR BQN
#> 10 EWR BTV
#> # … with 214 more rows
```
---
## Sampling rows
.pull-left[
`sample_n()`
```r
flights %>%
select(year, origin) %>%
sample_n(10)
```
```
#> # A tibble: 10 x 2
#> year origin
#> <int> <chr>
#> 1 2013 EWR
#> 2 2013 LGA
#> 3 2013 EWR
#> 4 2013 LGA
#> 5 2013 LGA
#> 6 2013 EWR
#> 7 2013 LGA
#> 8 2013 EWR
#> 9 2013 EWR
#> 10 2013 LGA
```
]
.pull-right[
`sample_frac()`
```r
flights %>%
select(year, origin) %>%
sample_frac(.00001)
```
```
#> # A tibble: 3 x 2
#> year origin
#> <int> <chr>
#> 1 2013 JFK
#> 2 2013 JFK
#> 3 2013 JFK
```
]
---
class: inverse, center, middle
# Exercises
---
## Data: Wake county parcels
Parcel boundaries with address and revenue-related information for properties in
Wake County, NC. http://data-wake.opendata.arcgis.com/datasets/parcels
.small[
```r
wake <- read_csv("https://www2.stat.duke.edu/~sms185/data/econ/parcels.csv")
```
]
---
.tiny[
```r
wake <-
janitor::clean_names(wake)
glimpse(wake)
```
```
#> Rows: 378,020
#> Columns: 59
#> $ objectid <dbl> 31257151, 31257152, 31257153, 31257154, 3125715…
#> $ pin_num <chr> "1701518493", "0745330365", "0753213162", "1743…
#> $ calc_area <dbl> 0.59611048, 0.06596296, 0.17740871, 0.24685172,…
#> $ reid <chr> "0004217", "0240874", "0337154", "0340605", "03…
#> $ map_name <chr> "1701 19", "0745 03", "0753 17", "1743 02", "18…
#> $ owner <chr> "HAMILTON, HUBERT EARL HAMILTON, PATRICIA Y", "…
#> $ addr1 <chr> "500 POPLAR DR", "104 MADISON GROVE PL", "3038 …
#> $ addr2 <chr> "RALEIGH NC 27603-4330", "CARY NC 27519-8159", …
#> $ addr3 <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ deed_book <chr> "002618", "015772", "012192", "016213", "014787…
#> $ deed_page <chr> "00683", "00810", "00556", "00085", "01020", "0…
#> $ deed_date <dttm> 1978-01-01, 2014-09-04, 2006-09-29, 2015-11-16…
#> $ deed_acres <dbl> 0.60, 0.07, 0.18, 0.25, 0.26, 0.16, 1.15, 1.01,…
#> $ bldg_val <dbl> 0, 190388, 319131, 216470, 390623, 204414, 2476…
#> $ land_val <dbl> 27500, 102200, 90000, 40000, 85000, 40000, 5600…
#> $ total_value_assd <dbl> 27500, 292588, 409131, 256470, 475623, 244414, …
#> $ billclass <dbl> 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 3, 2, 2, 2, 2, 2,…
#> $ billing_class_decode <chr> "Individual", "Individual", "Individual", "Indi…
#> $ propdesc <chr> "LO1 ECHO HTS SE7", "LO99 CARPENTER VILLAGE BLL…
#> $ heatedarea <dbl> NA, 1821, 3460, 2372, 3512, 2275, 2613, 1064, 3…
#> $ stname <chr> "HICKORY", "MADISON GROVE", "KILARNEY RIDGE", "…
#> $ stype <chr> "LN", "PL", "LOOP", "DR", "DR", "DR", "CT", "RD…
#> $ stpre <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ stsuf <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ stnum <dbl> 5626, 104, 3038, 4608, 1328, 4014, 1501, 8241, …
#> $ stmisc <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,…
#> $ site_address <chr> "5626 HICKORY LN", "104 MADISON GROVE PL", "303…
#> $ full_street_name <chr> "HICKORY LN", "MADISON GROVE PL", "KILARNEY RID…
#> $ city <chr> NA, "CAR", "CAR", "KNI", "WAK", "KNI", NA, NA, …
#> $ city_decode <chr> NA, "CARY", "CARY", "KNIGHTDALE", "WAKE FOREST"…
#> $ planning_jurisdiction <chr> "GA", "CA", "CA", "KN", "WF", "KN", "WC", "WC",…
#> $ township <chr> "16", "05", "04", "17", "19", "17", "15", "15",…
#> $ township_decode <chr> "St. Mary's", "CEDAR FORK", "CARY", "St. Matthe…
#> $ firedist <dbl> 23, NA, NA, NA, NA, NA, 23, 23, 23, 23, 23, 23,…
#> $ year_built <dbl> NA, 2007, 2006, 2009, 2011, 2007, 1991, 1994, 1…
#> $ totsalprice <dbl> 3000, 235000, 340000, 204000, 420000, 206500, 2…
#> $ sale_date <dttm> 1978-01-01, 2014-09-04, 2006-09-29, 2015-11-16…
#> $ type_and_use <chr> NA, "01", "01", "01", "01", "01", "01", "72", "…
#> $ type_use_decode <chr> NA, "SINGLFAM", "SINGLFAM", "SINGLFAM", "SINGLF…
#> $ designstyl <chr> NA, "TWH", "CVL", "CVL", "CVL", "CVL", "CVL", "…
#> $ design_style_decode <chr> NA, "Townhouse", "Conventional", "Conventional"…
#> $ units <dbl> 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,…
#> $ land_class <chr> "VAC", "RHS", "RHS", "RHS", "RHS", "RHS", "RHS"…
#> $ land_class_decode <chr> "Vacant", "Residential Less Than 10 Acres", "Re…
#> $ exemptdesc <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "CEMETE…
#> $ exemptstat <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, "782", …
#> $ ownership <dbl> 1010, 1010, 1010, 1010, 1010, 1010, 1010, 1020,…
#> $ activity <dbl> 9000, 1100, 1100, 1100, 1100, 1100, 1100, 1100,…
#> $ `function` <dbl> 9900, 1100, 1100, 1100, 1100, 1100, 1100, 1100,…
#> $ structure <dbl> 9000, 1140, 1110, 1110, 1110, 1110, 1110, 1152,…
#> $ site <dbl> 1000, 6100, 6100, 6100, 6100, 6100, 6100, 6100,…
#> $ totstructs <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
#> $ totunits <dbl> 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1,…
#> $ old_parcel_number <chr> "680-00000-0614", "--", "--", "--", "--", "--",…
#> $ zipnum <chr> "27603", "27519", "27511", "27545", "27587", "2…
#> $ shapearea <dbl> 25966.573, 2873.347, 7727.923, 10752.861, 11405…
#> $ shapelen <dbl> 690.3185, 237.6056, 381.0306, 486.3749, 441.330…
#> $ parcel_pk <dbl> 72379, 375431, 324111, 326889, 375146, 348747, …
#> $ land_code <chr> "V", "R", "R", "R", "R", "R", "R", "R", "R", "R…
```
]
---
## Tasks
1. Which city has the fewest land parcels in the dataset? *Hint*: `count()`.
<br><br>
2. Create a tibble that shows the year a parcel was built and the total
value, where all parcels are located in Apex and are more than one acre in
area. Sort the result in ascending order by year built.
<br><br>
3. Choose a subset of five variables and 10 random rows from `wake` and save it
as an object named `wake_mini`. Experiment renaming variables with
`select()` and `rename()` on `wake_mini`. What is the difference between the
two functions?
???
## Solutions
.solution[
```r
wake %>%
count(city_decode) %>%
arrange(n) %>%
slice(1)
```
```
#> # A tibble: 1 x 2
#> city_decode n
#> <chr> <int>
#> 1 CLAYTON 3
```