-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategorical-data-in-the-tidyverse.html
820 lines (779 loc) · 93.3 KB
/
categorical-data-in-the-tidyverse.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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>6 Categorical Data in the Tidyverse | R Programming Guidebook Project</title>
<meta name="description" content="6 Categorical Data in the Tidyverse | R Programming Guidebook Project" />
<meta name="generator" content="bookdown 0.26 and GitBook 2.6.7" />
<meta property="og:title" content="6 Categorical Data in the Tidyverse | R Programming Guidebook Project" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="6 Categorical Data in the Tidyverse | R Programming Guidebook Project" />
<meta name="author" content="Alec Nguyen" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="working-with-data-in-the-tidyverse.html"/>
<link rel="next" href="data-manipulation-with-dplyr.html"/>
<script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fuse.js@6.4.6/dist/fuse.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>About</a></li>
<li class="part"><span><b>I DataCamp</b></span></li>
<li class="chapter" data-level="1" data-path="introduction-to-r.html"><a href="introduction-to-r.html"><i class="fa fa-check"></i><b>1</b> Introduction to R</a>
<ul>
<li class="chapter" data-level="1.1" data-path="introduction-to-r.html"><a href="introduction-to-r.html#intro-to-basics"><i class="fa fa-check"></i><b>1.1</b> Intro to basics</a></li>
<li class="chapter" data-level="1.2" data-path="introduction-to-r.html"><a href="introduction-to-r.html#vectors"><i class="fa fa-check"></i><b>1.2</b> Vectors</a></li>
<li class="chapter" data-level="1.3" data-path="introduction-to-r.html"><a href="introduction-to-r.html#matrices"><i class="fa fa-check"></i><b>1.3</b> Matrices</a></li>
<li class="chapter" data-level="1.4" data-path="introduction-to-r.html"><a href="introduction-to-r.html#factors"><i class="fa fa-check"></i><b>1.4</b> Factors</a></li>
<li class="chapter" data-level="1.5" data-path="introduction-to-r.html"><a href="introduction-to-r.html#data-frames"><i class="fa fa-check"></i><b>1.5</b> Data frames</a></li>
<li class="chapter" data-level="1.6" data-path="introduction-to-r.html"><a href="introduction-to-r.html#lists"><i class="fa fa-check"></i><b>1.6</b> Lists</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="intermediate-r.html"><a href="intermediate-r.html"><i class="fa fa-check"></i><b>2</b> Intermediate R</a>
<ul>
<li class="chapter" data-level="2.1" data-path="intermediate-r.html"><a href="intermediate-r.html#conditionals-and-control-flow"><i class="fa fa-check"></i><b>2.1</b> Conditionals And Control Flow</a></li>
<li class="chapter" data-level="2.2" data-path="intermediate-r.html"><a href="intermediate-r.html#loops"><i class="fa fa-check"></i><b>2.2</b> Loops</a></li>
<li class="chapter" data-level="2.3" data-path="intermediate-r.html"><a href="intermediate-r.html#functions"><i class="fa fa-check"></i><b>2.3</b> Functions</a></li>
<li class="chapter" data-level="2.4" data-path="intermediate-r.html"><a href="intermediate-r.html#the-apply-family"><i class="fa fa-check"></i><b>2.4</b> The apply family</a></li>
<li class="chapter" data-level="2.5" data-path="intermediate-r.html"><a href="intermediate-r.html#utilities"><i class="fa fa-check"></i><b>2.5</b> Utilities</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html"><i class="fa fa-check"></i><b>3</b> Intro to the Tidyverse</a>
<ul>
<li class="chapter" data-level="3.1" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#data-wrangling"><i class="fa fa-check"></i><b>3.1</b> Data wrangling</a></li>
<li class="chapter" data-level="3.2" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#data-visualization"><i class="fa fa-check"></i><b>3.2</b> Data visualization</a></li>
<li class="chapter" data-level="3.3" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#grouping-and-summarizing"><i class="fa fa-check"></i><b>3.3</b> Grouping and summarizing</a></li>
<li class="chapter" data-level="3.4" data-path="intro-to-the-tidyverse.html"><a href="intro-to-the-tidyverse.html#types-of-visualizations"><i class="fa fa-check"></i><b>3.4</b> Types of visualizations</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html"><i class="fa fa-check"></i><b>4</b> Intro to Data Visualization with ggplot2</a>
<ul>
<li class="chapter" data-level="4.1" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#introduction"><i class="fa fa-check"></i><b>4.1</b> Introduction</a></li>
<li class="chapter" data-level="4.2" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#aesthetics"><i class="fa fa-check"></i><b>4.2</b> Aesthetics</a></li>
<li class="chapter" data-level="4.3" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#geometries"><i class="fa fa-check"></i><b>4.3</b> Geometries</a></li>
<li class="chapter" data-level="4.4" data-path="intro-to-data-visualization-with-ggplot2.html"><a href="intro-to-data-visualization-with-ggplot2.html#themes"><i class="fa fa-check"></i><b>4.4</b> Themes</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html"><i class="fa fa-check"></i><b>5</b> Working with Data in the Tidyverse</a>
<ul>
<li class="chapter" data-level="5.1" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#explore-your-data"><i class="fa fa-check"></i><b>5.1</b> Explore your data</a></li>
<li class="chapter" data-level="5.2" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#tame-your-data"><i class="fa fa-check"></i><b>5.2</b> Tame your data</a></li>
<li class="chapter" data-level="5.3" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#tidy-your-data"><i class="fa fa-check"></i><b>5.3</b> Tidy your data</a></li>
<li class="chapter" data-level="5.4" data-path="working-with-data-in-the-tidyverse.html"><a href="working-with-data-in-the-tidyverse.html#transform-your-data"><i class="fa fa-check"></i><b>5.4</b> Transform your data</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html"><i class="fa fa-check"></i><b>6</b> Categorical Data in the Tidyverse</a>
<ul>
<li class="chapter" data-level="6.1" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#introduction-to-factor-variables"><i class="fa fa-check"></i><b>6.1</b> Introduction to Factor Variables</a></li>
<li class="chapter" data-level="6.2" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#manipulating-factor-variables"><i class="fa fa-check"></i><b>6.2</b> Manipulating Factor Variables</a></li>
<li class="chapter" data-level="6.3" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#creating-factor-variables"><i class="fa fa-check"></i><b>6.3</b> Creating Factor Variables</a></li>
<li class="chapter" data-level="6.4" data-path="categorical-data-in-the-tidyverse.html"><a href="categorical-data-in-the-tidyverse.html#case-study-on-flight-etiquette"><i class="fa fa-check"></i><b>6.4</b> Case Study on Flight Etiquette</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html"><i class="fa fa-check"></i><b>7</b> Data Manipulation with dplyr</a>
<ul>
<li class="chapter" data-level="7.1" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#transforming-data-with-dplyr"><i class="fa fa-check"></i><b>7.1</b> Transforming Data with dplyr</a></li>
<li class="chapter" data-level="7.2" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#aggregating-data"><i class="fa fa-check"></i><b>7.2</b> Aggregating Data</a></li>
<li class="chapter" data-level="7.3" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#selecting-and-transforming-data"><i class="fa fa-check"></i><b>7.3</b> Selecting and Transforming Data</a></li>
<li class="chapter" data-level="7.4" data-path="data-manipulation-with-dplyr.html"><a href="data-manipulation-with-dplyr.html#case-study-the-babynames-dataset"><i class="fa fa-check"></i><b>7.4</b> Case Study: The babynames Dataset</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html"><i class="fa fa-check"></i><b>8</b> Joining Data with dplyr</a>
<ul>
<li class="chapter" data-level="8.1" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#joining-tables"><i class="fa fa-check"></i><b>8.1</b> Joining Tables</a></li>
<li class="chapter" data-level="8.2" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#left-and-right-joins"><i class="fa fa-check"></i><b>8.2</b> Left and Right Joins</a></li>
<li class="chapter" data-level="8.3" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#full-semi-and-anti-joins"><i class="fa fa-check"></i><b>8.3</b> Full, Semi, and Anti Joins</a></li>
<li class="chapter" data-level="8.4" data-path="joining-data-with-dplyr.html"><a href="joining-data-with-dplyr.html#case-study-joins-on-stack-overflow-data"><i class="fa fa-check"></i><b>8.4</b> Case Study: Joins on Stack Overflow Data</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html"><i class="fa fa-check"></i><b>9</b> Cleaning Data in R</a>
<ul>
<li class="chapter" data-level="9.1" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#common-data-problems"><i class="fa fa-check"></i><b>9.1</b> Common Data Problems</a></li>
<li class="chapter" data-level="9.2" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#categorical-and-text-data"><i class="fa fa-check"></i><b>9.2</b> Categorical and Text Data</a></li>
<li class="chapter" data-level="9.3" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#advanced-data-problems"><i class="fa fa-check"></i><b>9.3</b> Advanced Data Problems</a></li>
<li class="chapter" data-level="9.4" data-path="cleaning-data-in-r.html"><a href="cleaning-data-in-r.html#record-linkage"><i class="fa fa-check"></i><b>9.4</b> Record Linkage</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html"><i class="fa fa-check"></i><b>10</b> Introduction to SQL</a>
<ul>
<li class="chapter" data-level="10.1" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#selecting-columns"><i class="fa fa-check"></i><b>10.1</b> Selecting columns</a></li>
<li class="chapter" data-level="10.2" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#filtering-rows"><i class="fa fa-check"></i><b>10.2</b> Filtering rows</a></li>
<li class="chapter" data-level="10.3" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#aggregate-functions"><i class="fa fa-check"></i><b>10.3</b> Aggregate Functions</a></li>
<li class="chapter" data-level="10.4" data-path="introduction-to-sql.html"><a href="introduction-to-sql.html#sorting-and-grouping"><i class="fa fa-check"></i><b>10.4</b> Sorting and grouping</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html"><i class="fa fa-check"></i><b>11</b> Joining Data in SQL</a>
<ul>
<li class="chapter" data-level="11.1" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#introduction-to-joins"><i class="fa fa-check"></i><b>11.1</b> Introduction to joins</a></li>
<li class="chapter" data-level="11.2" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#outer-joins-and-cross-joins"><i class="fa fa-check"></i><b>11.2</b> Outer joins and cross joins</a></li>
<li class="chapter" data-level="11.3" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#set-theory-clauses"><i class="fa fa-check"></i><b>11.3</b> Set theory clauses</a></li>
<li class="chapter" data-level="11.4" data-path="joining-data-in-sql.html"><a href="joining-data-in-sql.html#subqueries"><i class="fa fa-check"></i><b>11.4</b> Subqueries</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html"><i class="fa fa-check"></i><b>12</b> Web Scraping in R</a>
<ul>
<li class="chapter" data-level="12.1" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#introduction-to-html-and-web-scraping"><i class="fa fa-check"></i><b>12.1</b> Introduction to HTML and Web Scraping</a></li>
<li class="chapter" data-level="12.2" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#navigation-and-selection-with-css"><i class="fa fa-check"></i><b>12.2</b> Navigation and Selection with CSS</a></li>
<li class="chapter" data-level="12.3" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#advanced-selection-with-xpath"><i class="fa fa-check"></i><b>12.3</b> Advanced Selection with XPATH</a></li>
<li class="chapter" data-level="12.4" data-path="web-scraping-in-r.html"><a href="web-scraping-in-r.html#scraping-best-practices"><i class="fa fa-check"></i><b>12.4</b> Scraping Best Practices</a></li>
</ul></li>
<li class="part"><span><b>II Econometrics</b></span></li>
<li class="chapter" data-level="13" data-path="ch-2---slr.html"><a href="ch-2---slr.html"><i class="fa fa-check"></i><b>13</b> Ch 2 - SLR</a>
<ul>
<li class="chapter" data-level="13.1" data-path="ch-2---slr.html"><a href="ch-2---slr.html#notes"><i class="fa fa-check"></i><b>13.1</b> Notes</a></li>
<li class="chapter" data-level="13.2" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-2.3-ceo-salary-and-return-on-equity"><i class="fa fa-check"></i><b>13.2</b> Example 2.3: CEO Salary and Return on Equity</a></li>
<li class="chapter" data-level="13.3" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-2.4-wage-and-education"><i class="fa fa-check"></i><b>13.3</b> Example 2.4: Wage and Education</a></li>
<li class="chapter" data-level="13.4" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-2.5-voting-outcomes-and-campaign-expenditures"><i class="fa fa-check"></i><b>13.4</b> Example 2.5: Voting Outcomes and Campaign Expenditures</a></li>
<li class="chapter" data-level="13.5" data-path="ch-2---slr.html"><a href="ch-2---slr.html#example-of-fitted-values-haty"><i class="fa fa-check"></i><b>13.5</b> Example of Fitted Values (<span class="math inline">\(\hat{y}\)</span>)</a></li>
</ul></li>
<li class="chapter" data-level="14" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html"><i class="fa fa-check"></i><b>14</b> Ch 3 - MLR</a>
<ul>
<li class="chapter" data-level="14.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#data"><i class="fa fa-check"></i><b>14.1</b> Data</a>
<ul>
<li class="chapter" data-level="14.1.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#summary-statistics"><i class="fa fa-check"></i><b>14.1.1</b> Summary Statistics</a></li>
</ul></li>
<li class="chapter" data-level="14.2" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#regression-model-comparisons"><i class="fa fa-check"></i><b>14.2</b> Regression model comparisons</a></li>
<li class="chapter" data-level="14.3" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#adjusted-r-squared"><i class="fa fa-check"></i><b>14.3</b> Adjusted R-Squared</a></li>
<li class="chapter" data-level="14.4" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#multicollinearity"><i class="fa fa-check"></i><b>14.4</b> Multicollinearity</a>
<ul>
<li class="chapter" data-level="14.4.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#variance-inflation-factor-vif"><i class="fa fa-check"></i><b>14.4.1</b> Variance Inflation Factor (VIF)</a></li>
<li class="chapter" data-level="14.4.2" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#joint-hypotheses-test"><i class="fa fa-check"></i><b>14.4.2</b> Joint hypotheses test</a></li>
</ul></li>
<li class="chapter" data-level="14.5" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#testing-linear-combinations-of-parameters"><i class="fa fa-check"></i><b>14.5</b> Testing linear combinations of parameters</a></li>
<li class="chapter" data-level="14.6" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#a-note-on-presentation"><i class="fa fa-check"></i><b>14.6</b> A note on presentation</a></li>
<li class="chapter" data-level="14.7" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#log-transformations"><i class="fa fa-check"></i><b>14.7</b> Log transformations</a>
<ul>
<li class="chapter" data-level="14.7.1" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#histograms"><i class="fa fa-check"></i><b>14.7.1</b> Histograms</a></li>
<li class="chapter" data-level="14.7.2" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#scatter-plots"><i class="fa fa-check"></i><b>14.7.2</b> Scatter plots</a></li>
<li class="chapter" data-level="14.7.3" data-path="ch-3---mlr.html"><a href="ch-3---mlr.html#regression-models-with-levels-and-logs"><i class="fa fa-check"></i><b>14.7.3</b> Regression models with levels and logs</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="15" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html"><i class="fa fa-check"></i><b>15</b> Dummy Variables Part 1</a>
<ul>
<li class="chapter" data-level="15.1" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#obtain-and-prepare-data"><i class="fa fa-check"></i><b>15.1</b> Obtain and prepare data</a></li>
<li class="chapter" data-level="15.2" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#define-dummy-variables"><i class="fa fa-check"></i><b>15.2</b> Define dummy variables</a>
<ul>
<li class="chapter" data-level="15.2.1" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#alpha-model"><i class="fa fa-check"></i><b>15.2.1</b> Alpha Model</a></li>
<li class="chapter" data-level="15.2.2" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#beta-model"><i class="fa fa-check"></i><b>15.2.2</b> Beta Model</a></li>
</ul></li>
<li class="chapter" data-level="15.3" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#compare-the-regressions-side-by-side"><i class="fa fa-check"></i><b>15.3</b> Compare the regressions side-by-side</a></li>
<li class="chapter" data-level="15.4" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#compare-the-predictions-of-each-model"><i class="fa fa-check"></i><b>15.4</b> Compare the predictions of each model</a>
<ul>
<li class="chapter" data-level="15.4.1" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#group-averages"><i class="fa fa-check"></i><b>15.4.1</b> Group averages</a></li>
<li class="chapter" data-level="15.4.2" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#causal-estimates"><i class="fa fa-check"></i><b>15.4.2</b> Causal estimates?</a></li>
<li class="chapter" data-level="15.4.3" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#what-about-age"><i class="fa fa-check"></i><b>15.4.3</b> What about age?</a></li>
<li class="chapter" data-level="15.4.4" data-path="dummy-variables-part-1.html"><a href="dummy-variables-part-1.html#what-about-sex"><i class="fa fa-check"></i><b>15.4.4</b> What about sex?</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="16" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html"><i class="fa fa-check"></i><b>16</b> Dummy Variables Part 2</a>
<ul>
<li class="chapter" data-level="16.1" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#size-only"><i class="fa fa-check"></i><b>16.1</b> Size only</a></li>
<li class="chapter" data-level="16.2" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#number-of-bathrooms-and-size"><i class="fa fa-check"></i><b>16.2</b> Number of bathrooms and size</a></li>
<li class="chapter" data-level="16.3" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#slope-dummy"><i class="fa fa-check"></i><b>16.3</b> Slope dummy</a></li>
<li class="chapter" data-level="16.4" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#intercept-and-slope-dummies"><i class="fa fa-check"></i><b>16.4</b> Intercept and slope dummies</a></li>
<li class="chapter" data-level="16.5" data-path="dummy-variables-part-2.html"><a href="dummy-variables-part-2.html#models-with-the-number-of-bedrooms"><i class="fa fa-check"></i><b>16.5</b> Models with the number of bedrooms</a></li>
</ul></li>
<li class="chapter" data-level="17" data-path="fixed-effects.html"><a href="fixed-effects.html"><i class="fa fa-check"></i><b>17</b> Fixed Effects</a>
<ul>
<li class="chapter" data-level="17.1" data-path="fixed-effects.html"><a href="fixed-effects.html#variables"><i class="fa fa-check"></i><b>17.1</b> Variables</a></li>
<li class="chapter" data-level="17.2" data-path="fixed-effects.html"><a href="fixed-effects.html#ols"><i class="fa fa-check"></i><b>17.2</b> OLS</a></li>
<li class="chapter" data-level="17.3" data-path="fixed-effects.html"><a href="fixed-effects.html#country-fixed-effects"><i class="fa fa-check"></i><b>17.3</b> Country Fixed Effects</a></li>
<li class="chapter" data-level="17.4" data-path="fixed-effects.html"><a href="fixed-effects.html#year-fixed-effects"><i class="fa fa-check"></i><b>17.4</b> Year Fixed Effects</a></li>
<li class="chapter" data-level="17.5" data-path="fixed-effects.html"><a href="fixed-effects.html#country-and-year-fixed-effects"><i class="fa fa-check"></i><b>17.5</b> Country and Year Fixed Effects</a></li>
<li class="chapter" data-level="17.6" data-path="fixed-effects.html"><a href="fixed-effects.html#comparison-of-all-models"><i class="fa fa-check"></i><b>17.6</b> Comparison of all models</a>
<ul>
<li class="chapter" data-level="17.6.1" data-path="fixed-effects.html"><a href="fixed-effects.html#within-transformation"><i class="fa fa-check"></i><b>17.6.1</b> Within Transformation</a></li>
<li class="chapter" data-level="17.6.2" data-path="fixed-effects.html"><a href="fixed-effects.html#plm-package"><i class="fa fa-check"></i><b>17.6.2</b> PLM Package</a></li>
<li class="chapter" data-level="17.6.3" data-path="fixed-effects.html"><a href="fixed-effects.html#dummy-variables"><i class="fa fa-check"></i><b>17.6.3</b> Dummy Variables</a></li>
</ul></li>
<li class="chapter" data-level="17.7" data-path="fixed-effects.html"><a href="fixed-effects.html#data-summary-by-country"><i class="fa fa-check"></i><b>17.7</b> Data Summary by Country</a>
<ul>
<li class="chapter" data-level="17.7.1" data-path="fixed-effects.html"><a href="fixed-effects.html#average-values-for-each-country"><i class="fa fa-check"></i><b>17.7.1</b> Average Values for Each Country</a></li>
<li class="chapter" data-level="17.7.2" data-path="fixed-effects.html"><a href="fixed-effects.html#variable-specific-values-and-within-transformation-for-each-country"><i class="fa fa-check"></i><b>17.7.2</b> Variable-Specific Values and Within Transformation for Each Country</a></li>
<li class="chapter" data-level="17.7.3" data-path="fixed-effects.html"><a href="fixed-effects.html#life-expectancy"><i class="fa fa-check"></i><b>17.7.3</b> Life expectancy</a></li>
<li class="chapter" data-level="17.7.4" data-path="fixed-effects.html"><a href="fixed-effects.html#gdp-per-capita"><i class="fa fa-check"></i><b>17.7.4</b> GDP per capita</a></li>
<li class="chapter" data-level="17.7.5" data-path="fixed-effects.html"><a href="fixed-effects.html#population"><i class="fa fa-check"></i><b>17.7.5</b> Population</a></li>
<li class="chapter" data-level="17.7.6" data-path="fixed-effects.html"><a href="fixed-effects.html#percent-female"><i class="fa fa-check"></i><b>17.7.6</b> Percent female</a></li>
<li class="chapter" data-level="17.7.7" data-path="fixed-effects.html"><a href="fixed-effects.html#percent-rural"><i class="fa fa-check"></i><b>17.7.7</b> Percent rural</a></li>
</ul></li>
<li class="chapter" data-level="17.8" data-path="fixed-effects.html"><a href="fixed-effects.html#bookdown-style-note"><i class="fa fa-check"></i><b>17.8</b> Bookdown Style Note</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="difference-in-differences.html"><a href="difference-in-differences.html"><i class="fa fa-check"></i><b>18</b> Difference-in-Differences</a>
<ul>
<li class="chapter" data-level="18.1" data-path="difference-in-differences.html"><a href="difference-in-differences.html#data-1"><i class="fa fa-check"></i><b>18.1</b> Data</a></li>
<li class="chapter" data-level="18.2" data-path="difference-in-differences.html"><a href="difference-in-differences.html#model-1"><i class="fa fa-check"></i><b>18.2</b> Model 1</a>
<ul>
<li class="chapter" data-level="18.2.1" data-path="difference-in-differences.html"><a href="difference-in-differences.html#equivalent-model-1"><i class="fa fa-check"></i><b>18.2.1</b> Equivalent model 1</a></li>
</ul></li>
<li class="chapter" data-level="18.3" data-path="difference-in-differences.html"><a href="difference-in-differences.html#model-2"><i class="fa fa-check"></i><b>18.3</b> Model 2</a></li>
<li class="chapter" data-level="18.4" data-path="difference-in-differences.html"><a href="difference-in-differences.html#comparison-of-models"><i class="fa fa-check"></i><b>18.4</b> Comparison of models</a></li>
<li class="chapter" data-level="18.5" data-path="difference-in-differences.html"><a href="difference-in-differences.html#additional-questions"><i class="fa fa-check"></i><b>18.5</b> Additional questions</a>
<ul>
<li class="chapter" data-level="18.5.1" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-1"><i class="fa fa-check"></i><b>18.5.1</b> Question 1</a></li>
<li class="chapter" data-level="18.5.2" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-2"><i class="fa fa-check"></i><b>18.5.2</b> Question 2</a></li>
<li class="chapter" data-level="18.5.3" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-3"><i class="fa fa-check"></i><b>18.5.3</b> Question 3</a></li>
<li class="chapter" data-level="18.5.4" data-path="difference-in-differences.html"><a href="difference-in-differences.html#question-4"><i class="fa fa-check"></i><b>18.5.4</b> Question 4</a></li>
</ul></li>
<li class="chapter" data-level="18.6" data-path="difference-in-differences.html"><a href="difference-in-differences.html#polynomials"><i class="fa fa-check"></i><b>18.6</b> Polynomials</a></li>
</ul></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">R Programming Guidebook Project</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="categorical-data-in-the-tidyverse" class="section level1 hasAnchor" number="6">
<h1><span class="header-section-number">6</span> Categorical Data in the Tidyverse<a href="categorical-data-in-the-tidyverse.html#categorical-data-in-the-tidyverse" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p><a href="https://learn.datacamp.com/courses/categorical-data-in-the-tidyverse" class="uri">https://learn.datacamp.com/courses/categorical-data-in-the-tidyverse</a></p>
<p>Required packages to load:</p>
<div class="sourceCode" id="cb320"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb320-1"><a href="categorical-data-in-the-tidyverse.html#cb320-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(ggplot2)</span>
<span id="cb320-2"><a href="categorical-data-in-the-tidyverse.html#cb320-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span></code></pre></div>
<pre><code>##
## Attaching package: 'dplyr'</code></pre>
<pre><code>## The following objects are masked from 'package:stats':
##
## filter, lag</code></pre>
<pre><code>## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union</code></pre>
<div class="sourceCode" id="cb324"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb324-1"><a href="categorical-data-in-the-tidyverse.html#cb324-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyr)</span>
<span id="cb324-2"><a href="categorical-data-in-the-tidyverse.html#cb324-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(readr)</span>
<span id="cb324-3"><a href="categorical-data-in-the-tidyverse.html#cb324-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(forcats)</span>
<span id="cb324-4"><a href="categorical-data-in-the-tidyverse.html#cb324-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(stringr)</span>
<span id="cb324-5"><a href="categorical-data-in-the-tidyverse.html#cb324-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(scales)</span></code></pre></div>
<pre><code>##
## Attaching package: 'scales'</code></pre>
<pre><code>## The following object is masked from 'package:readr':
##
## col_factor</code></pre>
<div id="introduction-to-factor-variables" class="section level2 hasAnchor" number="6.1">
<h2><span class="header-section-number">6.1</span> Introduction to Factor Variables<a href="categorical-data-in-the-tidyverse.html#introduction-to-factor-variables" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>Getting Number Of Levels</strong></p>
<p><code>mutate()</code> and <code>summarise()</code> in <code>dplyr</code> both have variants to add the suffix <code>if</code> or <code>all</code> to change the operation. <code>mutate_if()</code> and <code>summarise_if</code> apply their “second argument”-a function, to all columns where the “first argument” is true, and <code>mutate_all()</code> and <code>summarise_all</code> take one argument, a function, and apply it to all columns.</p>
<div class="sourceCode" id="cb327"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb327-1"><a href="categorical-data-in-the-tidyverse.html#cb327-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Change all the character columns to factors</span></span>
<span id="cb327-2"><a href="categorical-data-in-the-tidyverse.html#cb327-2" aria-hidden="true" tabindex="-1"></a>responses_as_factors <span class="ot"><-</span> multiple_choice_responses <span class="sc">%>%</span></span>
<span id="cb327-3"><a href="categorical-data-in-the-tidyverse.html#cb327-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate_if</span>(is.character, as.factor)</span>
<span id="cb327-4"><a href="categorical-data-in-the-tidyverse.html#cb327-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb327-5"><a href="categorical-data-in-the-tidyverse.html#cb327-5" aria-hidden="true" tabindex="-1"></a>number_of_levels <span class="ot"><-</span> responses_as_factors <span class="sc">%>%</span></span>
<span id="cb327-6"><a href="categorical-data-in-the-tidyverse.html#cb327-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># apply the function nlevels to each column</span></span>
<span id="cb327-7"><a href="categorical-data-in-the-tidyverse.html#cb327-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise_all</span>(nlevels) <span class="sc">%>%</span></span>
<span id="cb327-8"><a href="categorical-data-in-the-tidyverse.html#cb327-8" aria-hidden="true" tabindex="-1"></a> <span class="co"># change the dataset from wide to long</span></span>
<span id="cb327-9"><a href="categorical-data-in-the-tidyverse.html#cb327-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(variable, num_levels)</span>
<span id="cb327-10"><a href="categorical-data-in-the-tidyverse.html#cb327-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb327-11"><a href="categorical-data-in-the-tidyverse.html#cb327-11" aria-hidden="true" tabindex="-1"></a>number_of_levels[<span class="dv">1</span><span class="sc">:</span><span class="dv">8</span>,]</span></code></pre></div>
<pre><code>## # A tibble: 8 × 2
## variable num_levels
## <chr> <int>
## 1 LearningPlatformUsefulnessArxiv 2
## 2 LearningPlatformUsefulnessBlogs 2
## 3 LearningPlatformUsefulnessCollege 2
## 4 LearningPlatformUsefulnessCompany 1
## 5 LearningPlatformUsefulnessConferences 2
## 6 LearningPlatformUsefulnessFriends 1
## 7 LearningPlatformUsefulnessKaggle 2
## 8 LearningPlatformUsefulnessNewsletters 1</code></pre>
<p><strong>Examining Number Of Levels</strong></p>
<p><code>dplyr</code> has two other functions to explore a dataset. The first is <code>top_n(x, var)</code>, which give the first <code>x</code> rows of a dataset based on the value of <code>var.</code></p>
<div class="sourceCode" id="cb329"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb329-1"><a href="categorical-data-in-the-tidyverse.html#cb329-1" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%>%</span></span>
<span id="cb329-2"><a href="categorical-data-in-the-tidyverse.html#cb329-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">top_n</span>(<span class="dv">2</span>, mpg)</span></code></pre></div>
<pre><code>## mpg cyl disp hp drat wt qsec vs am gear carb
## Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
## Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1</code></pre>
<p>The other is<code>pull()</code>, which allows extraction of a column and take out the name, leaving only the value(s) from the column.</p>
<p><code>pull</code> can be used interchangeably with<code>$</code>as a way to extract a column as a vector from a table. For example, <code>mean(mtcars$mpg)</code> is the same as <code>mean(mtcars %>% pull(mpg))</code> or <code>mtcars %>% pull(mpg) %>% mean()</code>.</p>
<div class="sourceCode" id="cb331"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb331-1"><a href="categorical-data-in-the-tidyverse.html#cb331-1" aria-hidden="true" tabindex="-1"></a>mtcars <span class="sc">%>%</span></span>
<span id="cb331-2"><a href="categorical-data-in-the-tidyverse.html#cb331-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">top_n</span>(<span class="dv">2</span>, mpg) <span class="sc">%>%</span></span>
<span id="cb331-3"><a href="categorical-data-in-the-tidyverse.html#cb331-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(mpg)</span></code></pre></div>
<pre><code>## [1] 32.4 33.9</code></pre>
<p><strong>Reordering A Variable By Its Frequency</strong></p>
<p>One of the primary purposes of the <code>forcats</code> package is to make it easy to quickly change visualizations when working with qualitative variables.</p>
<p>Flip the coordinates using <code>coord_flip()</code> to make it more readable.</p>
<p>Use <code>fct_infreq()</code> to make the bar plot ordered by frequency.</p>
<p>Use <code>fct_rev()</code> to reverse the order of a factor.</p>
<p>Use <code>fct_reorder()</code> from <code>forcats</code> to take one variable and order it by another.</p>
<div class="sourceCode" id="cb333"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb333-1"><a href="categorical-data-in-the-tidyverse.html#cb333-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Make a bar plot </span></span>
<span id="cb333-2"><a href="categorical-data-in-the-tidyverse.html#cb333-2" aria-hidden="true" tabindex="-1"></a><span class="fu">ggplot</span>(multiple_choice_responses, <span class="fu">aes</span>(<span class="at">x =</span> <span class="fu">fct_rev</span>(<span class="fu">fct_infreq</span>(EmployerIndustry)))) <span class="sc">+</span> </span>
<span id="cb333-3"><a href="categorical-data-in-the-tidyverse.html#cb333-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>() <span class="sc">+</span> </span>
<span id="cb333-4"><a href="categorical-data-in-the-tidyverse.html#cb333-4" aria-hidden="true" tabindex="-1"></a><span class="co"># flip the coordinates</span></span>
<span id="cb333-5"><a href="categorical-data-in-the-tidyverse.html#cb333-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>()</span></code></pre></div>
<p><img src="06-Categorical_Data_in_Tidyverse_files/figure-html/unnamed-chunk-6-1.png" width="672" /></p>
</div>
<div id="manipulating-factor-variables" class="section level2 hasAnchor" number="6.2">
<h2><span class="header-section-number">6.2</span> Manipulating Factor Variables<a href="categorical-data-in-the-tidyverse.html#manipulating-factor-variables" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p><strong>How To Change The Order Of Levels</strong></p>
<p>Use <code>fct_relevel()</code> to change the order of factor levels.</p>
<div class="sourceCode" id="cb334"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb334-1"><a href="categorical-data-in-the-tidyverse.html#cb334-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the levels of WorkInternalVsExternalTools</span></span>
<span id="cb334-2"><a href="categorical-data-in-the-tidyverse.html#cb334-2" aria-hidden="true" tabindex="-1"></a><span class="fu">levels</span>(responses_data<span class="sc">$</span>WorkInternalVsExternalTools)</span></code></pre></div>
<pre><code>## [1] "Approximately half internal and half external"
## [2] "Do not know"
## [3] "Entirely external"
## [4] "Entirely internal"
## [5] "More external than internal"
## [6] "More internal than external"</code></pre>
<div class="sourceCode" id="cb336"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb336-1"><a href="categorical-data-in-the-tidyverse.html#cb336-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Reorder the levels from internal to external </span></span>
<span id="cb336-2"><a href="categorical-data-in-the-tidyverse.html#cb336-2" aria-hidden="true" tabindex="-1"></a>reordered_respsonses <span class="ot"><-</span> responses_data[<span class="dv">1</span><span class="sc">:</span><span class="dv">8</span>,] <span class="sc">%>%</span></span>
<span id="cb336-3"><a href="categorical-data-in-the-tidyverse.html#cb336-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">WorkInternalVsExternalTools =</span> <span class="fu">fct_relevel</span>(WorkInternalVsExternalTools,</span>
<span id="cb336-4"><a href="categorical-data-in-the-tidyverse.html#cb336-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Entirely internal"</span>, </span>
<span id="cb336-5"><a href="categorical-data-in-the-tidyverse.html#cb336-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"More internal than external"</span>,</span>
<span id="cb336-6"><a href="categorical-data-in-the-tidyverse.html#cb336-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Approximately half internal and half external"</span>,</span>
<span id="cb336-7"><a href="categorical-data-in-the-tidyverse.html#cb336-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"More external than internal"</span>, </span>
<span id="cb336-8"><a href="categorical-data-in-the-tidyverse.html#cb336-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"Entirely external"</span>,</span>
<span id="cb336-9"><a href="categorical-data-in-the-tidyverse.html#cb336-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"Do not know"</span>))</span></code></pre></div>
<p>Sometimes, a data have many levels and only need to move one. Instead of writing out all the levels, use some arguments in <code>fct_relevel()</code> to save time.</p>
<p>In the code below, using <code>after = Inf</code> will move the specified level to the last, if <code>after = n</code>, in which <code>n</code> is a number, then that specified level will the reordered to after that <code>n</code>th place.</p>
<div class="sourceCode" id="cb337"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb337-1"><a href="categorical-data-in-the-tidyverse.html#cb337-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Reorder the levels from internal to external </span></span>
<span id="cb337-2"><a href="categorical-data-in-the-tidyverse.html#cb337-2" aria-hidden="true" tabindex="-1"></a>somwhat_reordered <span class="ot"><-</span> responses_data <span class="sc">%>%</span></span>
<span id="cb337-3"><a href="categorical-data-in-the-tidyverse.html#cb337-3" aria-hidden="true" tabindex="-1"></a><span class="co"># Move "Do not know" to be the last level.</span></span>
<span id="cb337-4"><a href="categorical-data-in-the-tidyverse.html#cb337-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">WorkInternalVsExternalTools =</span> <span class="fu">fct_relevel</span>(WorkInternalVsExternalTools, <span class="st">"Do not know"</span>, <span class="at">after =</span> <span class="cn">Inf</span>)) <span class="sc">%>%</span></span>
<span id="cb337-5"><a href="categorical-data-in-the-tidyverse.html#cb337-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Move "Entirely internal" and ""Approximately half internal and half external" to the front</span></span>
<span id="cb337-6"><a href="categorical-data-in-the-tidyverse.html#cb337-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">WorkInternalVsExternalTools =</span> <span class="fu">fct_relevel</span>(WorkInternalVsExternalTools, <span class="st">"Entirely internal"</span>, <span class="st">"Approximately half internal and half external"</span>)) </span>
<span id="cb337-7"><a href="categorical-data-in-the-tidyverse.html#cb337-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb337-8"><a href="categorical-data-in-the-tidyverse.html#cb337-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the levels of WorkInternalVsExternalTools</span></span>
<span id="cb337-9"><a href="categorical-data-in-the-tidyverse.html#cb337-9" aria-hidden="true" tabindex="-1"></a><span class="fu">levels</span>(somwhat_reordered<span class="sc">$</span>WorkInternalVsExternalTools) </span></code></pre></div>
<pre><code>## [1] "Entirely internal"
## [2] "Approximately half internal and half external"
## [3] "Entirely external"
## [4] "More external than internal"
## [5] "More internal than external"
## [6] "Do not know"</code></pre>
<p><strong>Renaming Levels</strong></p>
<p>Use <code>fct_recode()</code> to rename levels, with the new level name on the left, and the old on the right:</p>
<div class="sourceCode" id="cb339"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb339-1"><a href="categorical-data-in-the-tidyverse.html#cb339-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Reorder the levels from internal to external </span></span>
<span id="cb339-2"><a href="categorical-data-in-the-tidyverse.html#cb339-2" aria-hidden="true" tabindex="-1"></a>renamed_respsonses <span class="ot"><-</span> responses_data[<span class="dv">1</span><span class="sc">:</span><span class="dv">8</span>,] <span class="sc">%>%</span></span>
<span id="cb339-3"><a href="categorical-data-in-the-tidyverse.html#cb339-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">WorkInternalVsExternalTools =</span> <span class="fu">fct_recode</span>(WorkInternalVsExternalTools,</span>
<span id="cb339-4"><a href="categorical-data-in-the-tidyverse.html#cb339-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Entirely In"</span> <span class="ot">=</span> <span class="st">"Entirely internal"</span>, </span>
<span id="cb339-5"><a href="categorical-data-in-the-tidyverse.html#cb339-5" aria-hidden="true" tabindex="-1"></a> <span class="st">"More In than Out"</span> <span class="ot">=</span> <span class="st">"More internal than external"</span>,</span>
<span id="cb339-6"><a href="categorical-data-in-the-tidyverse.html#cb339-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Hald In and Out"</span> <span class="ot">=</span> <span class="st">"Approximately half internal and half external"</span>,</span>
<span id="cb339-7"><a href="categorical-data-in-the-tidyverse.html#cb339-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"More Out than In"</span> <span class="ot">=</span> <span class="st">"More external than internal"</span>, </span>
<span id="cb339-8"><a href="categorical-data-in-the-tidyverse.html#cb339-8" aria-hidden="true" tabindex="-1"></a> <span class="st">"Entirely Out"</span> <span class="ot">=</span> <span class="st">"Entirely external"</span>,</span>
<span id="cb339-9"><a href="categorical-data-in-the-tidyverse.html#cb339-9" aria-hidden="true" tabindex="-1"></a> <span class="st">"IDK"</span> <span class="ot">=</span> <span class="st">"Do not know"</span>))</span>
<span id="cb339-10"><a href="categorical-data-in-the-tidyverse.html#cb339-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb339-11"><a href="categorical-data-in-the-tidyverse.html#cb339-11" aria-hidden="true" tabindex="-1"></a><span class="co"># Get the levels of WorkInternalVsExternalTools</span></span>
<span id="cb339-12"><a href="categorical-data-in-the-tidyverse.html#cb339-12" aria-hidden="true" tabindex="-1"></a><span class="fu">levels</span>(renamed_respsonses<span class="sc">$</span>WorkInternalVsExternalTools)</span></code></pre></div>
<pre><code>## [1] "Hald In and Out" "IDK" "Entirely Out" "Entirely In"
## [5] "More Out than In" "More In than Out"</code></pre>
<p><strong>How To Collapse/Merge Levels</strong></p>
<p>Sometimes, a dataset would have too many levels that ties to a general category, so merging those levels into one umbrella level would be easier to the eyes. Use <code>fct_collapse()</code> to collapse the levels into one.</p>
<p>After collapsing the desired levels into one, there would non-essential levels, classifying those unnecessary levels into <code>other</code> level would be ideal. Use <code>fct_other()</code> to turn other levels into <code>other</code>(use <code>keep =</code> to retain the umbrella created titles)</p>
<div class="sourceCode" id="cb341"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb341-1"><a href="categorical-data-in-the-tidyverse.html#cb341-1" aria-hidden="true" tabindex="-1"></a>responses_data <span class="sc">%>%</span></span>
<span id="cb341-2"><a href="categorical-data-in-the-tidyverse.html#cb341-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Create new variable, grouped_titles, by collapsing levels in CurrentJobTitleSelect</span></span>
<span id="cb341-3"><a href="categorical-data-in-the-tidyverse.html#cb341-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">grouped_titles =</span> <span class="fu">fct_collapse</span>(CurrentJobTitleSelect, </span>
<span id="cb341-4"><a href="categorical-data-in-the-tidyverse.html#cb341-4" aria-hidden="true" tabindex="-1"></a> <span class="st">"Computer Scientist"</span> <span class="ot">=</span> <span class="st">"Software Developer/Software Engineer"</span>, </span>
<span id="cb341-5"><a href="categorical-data-in-the-tidyverse.html#cb341-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb341-6"><a href="categorical-data-in-the-tidyverse.html#cb341-6" aria-hidden="true" tabindex="-1"></a> <span class="st">"Data Analyst/Scientist/Engineer"</span> <span class="ot">=</span> <span class="fu">c</span>(<span class="st">"DBA/Database Engineer"</span>, <span class="st">"Data Scientist"</span>, </span>
<span id="cb341-7"><a href="categorical-data-in-the-tidyverse.html#cb341-7" aria-hidden="true" tabindex="-1"></a> <span class="st">"Business Analyst"</span>))) <span class="sc">%>%</span></span>
<span id="cb341-8"><a href="categorical-data-in-the-tidyverse.html#cb341-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Keep all the new titles and turn every other title into "Other"</span></span>
<span id="cb341-9"><a href="categorical-data-in-the-tidyverse.html#cb341-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">grouped_titles =</span> <span class="fu">fct_other</span>(grouped_titles, </span>
<span id="cb341-10"><a href="categorical-data-in-the-tidyverse.html#cb341-10" aria-hidden="true" tabindex="-1"></a> <span class="at">keep =</span> <span class="fu">c</span>(<span class="st">"Computer Scientist"</span>,</span>
<span id="cb341-11"><a href="categorical-data-in-the-tidyverse.html#cb341-11" aria-hidden="true" tabindex="-1"></a> <span class="st">"Data Analyst/Scientist/Engineer"</span>))) <span class="sc">%>%</span> </span>
<span id="cb341-12"><a href="categorical-data-in-the-tidyverse.html#cb341-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Get a count of the grouped titles</span></span>
<span id="cb341-13"><a href="categorical-data-in-the-tidyverse.html#cb341-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(grouped_titles)</span></code></pre></div>
<pre><code>## # A tibble: 4 × 2
## grouped_titles n
## <fct> <int>
## 1 Data Analyst/Scientist/Engineer 5
## 2 Computer Scientist 5
## 3 Other 2
## 4 <NA> 8</code></pre>
<p><strong>Showing Levels By Proportion</strong></p>
<p>Use <code>fct_lump()</code> to show only the selected levels that exceed the specified proportion(percentage of that level in the dataset), otherwise, collapse all the non-qualified levels into <code>other</code>).</p>
<div class="sourceCode" id="cb343"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb343-1"><a href="categorical-data-in-the-tidyverse.html#cb343-1" aria-hidden="true" tabindex="-1"></a>responses_data <span class="sc">%>%</span></span>
<span id="cb343-2"><a href="categorical-data-in-the-tidyverse.html#cb343-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># remove NAs of MLMethodNextYearSelect</span></span>
<span id="cb343-3"><a href="categorical-data-in-the-tidyverse.html#cb343-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(MLMethodNextYearSelect)) <span class="sc">%>%</span></span>
<span id="cb343-4"><a href="categorical-data-in-the-tidyverse.html#cb343-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># create ml_method, which lumps all those with less than 5% of people into "Other"</span></span>
<span id="cb343-5"><a href="categorical-data-in-the-tidyverse.html#cb343-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">ml_method =</span> <span class="fu">fct_lump</span>(MLMethodNextYearSelect, <span class="at">prop =</span> .<span class="dv">11</span>)) <span class="sc">%>%</span></span>
<span id="cb343-6"><a href="categorical-data-in-the-tidyverse.html#cb343-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># count the frequency of your new variable, sorted in descending order</span></span>
<span id="cb343-7"><a href="categorical-data-in-the-tidyverse.html#cb343-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(ml_method, <span class="at">sort =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<pre><code>## # A tibble: 5 × 2
## ml_method n
## <fct> <int>
## 1 Deep learning 6
## 2 Other 4
## 3 Neural Nets 2
## 4 Random Forests 2
## 5 Text Mining 2</code></pre>
<p><strong>Showing The “N” Number Most Common Levels</strong></p>
<p>Instead of showing levels by proportion but to show the most common <code>n</code> levels, use:</p>
<pre><code>fct_lump(dataset_name, n = __, other_level = "title_of_non-qualified_levels")</code></pre>
<p>For example:</p>
<div class="sourceCode" id="cb346"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb346-1"><a href="categorical-data-in-the-tidyverse.html#cb346-1" aria-hidden="true" tabindex="-1"></a>responses_data <span class="sc">%>%</span></span>
<span id="cb346-2"><a href="categorical-data-in-the-tidyverse.html#cb346-2" aria-hidden="true" tabindex="-1"></a><span class="co"># remove NAs </span></span>
<span id="cb346-3"><a href="categorical-data-in-the-tidyverse.html#cb346-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(MLMethodNextYearSelect)) <span class="sc">%>%</span></span>
<span id="cb346-4"><a href="categorical-data-in-the-tidyverse.html#cb346-4" aria-hidden="true" tabindex="-1"></a><span class="co"># create ml_method, retaining the first most common method and renaming others "other method" </span></span>
<span id="cb346-5"><a href="categorical-data-in-the-tidyverse.html#cb346-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">ml_method =</span> <span class="fu">fct_lump</span>(MLMethodNextYearSelect, <span class="at">n =</span> <span class="dv">1</span>, <span class="at">other_level =</span> <span class="st">"other method"</span>)) <span class="sc">%>%</span></span>
<span id="cb346-6"><a href="categorical-data-in-the-tidyverse.html#cb346-6" aria-hidden="true" tabindex="-1"></a><span class="co"># count the frequency of your new variable, sorted in descending order</span></span>
<span id="cb346-7"><a href="categorical-data-in-the-tidyverse.html#cb346-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(ml_method, <span class="at">sort =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
<pre><code>## # A tibble: 2 × 2
## ml_method n
## <fct> <int>
## 1 other method 10
## 2 Deep learning 6</code></pre>
</div>
<div id="creating-factor-variables" class="section level2 hasAnchor" number="6.3">
<h2><span class="header-section-number">6.3</span> Creating Factor Variables<a href="categorical-data-in-the-tidyverse.html#creating-factor-variables" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Use <code>str_remove()</code> from the <code>stringr</code> package to remove a certain phrase from observations in a column:</p>
<div class="sourceCode" id="cb348"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb348-1"><a href="categorical-data-in-the-tidyverse.html#cb348-1" aria-hidden="true" tabindex="-1"></a>platform_usefulness_long <span class="ot"><-</span> responses_data <span class="sc">%>%</span></span>
<span id="cb348-2"><a href="categorical-data-in-the-tidyverse.html#cb348-2" aria-hidden="true" tabindex="-1"></a><span class="co"># select columns with LearningPlatformUsefulness in title</span></span>
<span id="cb348-3"><a href="categorical-data-in-the-tidyverse.html#cb348-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">contains</span>(<span class="st">"LearningPlatformUsefulness"</span>)) <span class="sc">%>%</span></span>
<span id="cb348-4"><a href="categorical-data-in-the-tidyverse.html#cb348-4" aria-hidden="true" tabindex="-1"></a><span class="co"># change data from wide to long</span></span>
<span id="cb348-5"><a href="categorical-data-in-the-tidyverse.html#cb348-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(learning_platform, usefulness) <span class="sc">%>%</span></span>
<span id="cb348-6"><a href="categorical-data-in-the-tidyverse.html#cb348-6" aria-hidden="true" tabindex="-1"></a><span class="co"># remove rows where usefulness is NA</span></span>
<span id="cb348-7"><a href="categorical-data-in-the-tidyverse.html#cb348-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(usefulness))</span>
<span id="cb348-8"><a href="categorical-data-in-the-tidyverse.html#cb348-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb348-9"><a href="categorical-data-in-the-tidyverse.html#cb348-9" aria-hidden="true" tabindex="-1"></a>platform_usefulness_long[<span class="dv">1</span><span class="sc">:</span><span class="dv">7</span>,]</span></code></pre></div>
<pre><code>## # A tibble: 7 × 2
## learning_platform usefulness
## <chr> <chr>
## 1 LearningPlatformUsefulnessArxiv Very useful
## 2 LearningPlatformUsefulnessArxiv Very useful
## 3 LearningPlatformUsefulnessArxiv Somewhat useful
## 4 LearningPlatformUsefulnessArxiv Somewhat useful
## 5 LearningPlatformUsefulnessBlogs Very useful
## 6 LearningPlatformUsefulnessBlogs Somewhat useful
## 7 LearningPlatformUsefulnessBlogs Very useful</code></pre>
<div class="sourceCode" id="cb350"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb350-1"><a href="categorical-data-in-the-tidyverse.html#cb350-1" aria-hidden="true" tabindex="-1"></a><span class="co"># remove "LearningPlatformUsefulness" from each string in learning_platform </span></span>
<span id="cb350-2"><a href="categorical-data-in-the-tidyverse.html#cb350-2" aria-hidden="true" tabindex="-1"></a>platform_usefulness_short <span class="ot"><-</span> platform_usefulness_long <span class="sc">%>%</span></span>
<span id="cb350-3"><a href="categorical-data-in-the-tidyverse.html#cb350-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">learning_platform =</span> <span class="fu">str_remove</span>(learning_platform, <span class="st">"LearningPlatformUsefulness"</span>))</span>
<span id="cb350-4"><a href="categorical-data-in-the-tidyverse.html#cb350-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb350-5"><a href="categorical-data-in-the-tidyverse.html#cb350-5" aria-hidden="true" tabindex="-1"></a>platform_usefulness_short[<span class="dv">1</span><span class="sc">:</span><span class="dv">7</span>,]</span></code></pre></div>
<pre><code>## # A tibble: 7 × 2
## learning_platform usefulness
## <chr> <chr>
## 1 Arxiv Very useful
## 2 Arxiv Very useful
## 3 Arxiv Somewhat useful
## 4 Arxiv Somewhat useful
## 5 Blogs Very useful
## 6 Blogs Somewhat useful
## 7 Blogs Very useful</code></pre>
<p><strong>How To Add A Counted Column With “add+count()”</strong></p>
<p><code>add_count()</code> adds a column to the dataset, <code>n</code>, keeping the same number of rows as the original dataset. Just like <code>count()</code>, <code>n</code> defaults to be the number of rows for each group. Set <code>wt</code> equal to another column to make <code>n</code> equal to the sum of that column for each group:</p>
<div class="sourceCode" id="cb352"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb352-1"><a href="categorical-data-in-the-tidyverse.html#cb352-1" aria-hidden="true" tabindex="-1"></a>perc_useful_platform <span class="ot"><-</span> platform_usefulness_short <span class="sc">%>%</span></span>
<span id="cb352-2"><a href="categorical-data-in-the-tidyverse.html#cb352-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># change dataset to one row per learning_platform usefulness pair with number of entries for each</span></span>
<span id="cb352-3"><a href="categorical-data-in-the-tidyverse.html#cb352-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(learning_platform, usefulness) <span class="sc">%>%</span></span>
<span id="cb352-4"><a href="categorical-data-in-the-tidyverse.html#cb352-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># use add_count to create column with total number of answers for that learning_platform</span></span>
<span id="cb352-5"><a href="categorical-data-in-the-tidyverse.html#cb352-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_count</span>(learning_platform, <span class="at">wt =</span> n, <span class="at">name=</span><span class="st">'nn'</span>) <span class="sc">%>%</span></span>
<span id="cb352-6"><a href="categorical-data-in-the-tidyverse.html#cb352-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># create a new column, perc, that is the percentage of people giving that response for that learning_platform</span></span>
<span id="cb352-7"><a href="categorical-data-in-the-tidyverse.html#cb352-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">perc =</span> n <span class="sc">/</span> nn)</span>
<span id="cb352-8"><a href="categorical-data-in-the-tidyverse.html#cb352-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb352-9"><a href="categorical-data-in-the-tidyverse.html#cb352-9" aria-hidden="true" tabindex="-1"></a>perc_useful_platform[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>,]</span></code></pre></div>
<pre><code>## # A tibble: 5 × 5
## learning_platform usefulness n nn perc
## <chr> <chr> <int> <int> <dbl>
## 1 Arxiv Somewhat useful 2 4 0.5
## 2 Arxiv Very useful 2 4 0.5
## 3 Blogs Somewhat useful 2 5 0.4
## 4 Blogs Very useful 3 5 0.6
## 5 College Somewhat useful 2 5 0.4</code></pre>
<p><strong>Changing And Creating Variables With “case_when()”</strong></p>
<p>Make a new categorical variable/column base on another column using <code>case_when()</code>:</p>
<div class="sourceCode" id="cb354"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb354-1"><a href="categorical-data-in-the-tidyverse.html#cb354-1" aria-hidden="true" tabindex="-1"></a>mtcars[<span class="dv">1</span><span class="sc">:</span><span class="dv">7</span>,] <span class="sc">%>%</span></span>
<span id="cb354-2"><a href="categorical-data-in-the-tidyverse.html#cb354-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Create the speed column/variable based on hp (horse power)</span></span>
<span id="cb354-3"><a href="categorical-data-in-the-tidyverse.html#cb354-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">speed =</span> <span class="fu">case_when</span>(</span>
<span id="cb354-4"><a href="categorical-data-in-the-tidyverse.html#cb354-4" aria-hidden="true" tabindex="-1"></a> hp <span class="sc"><</span> <span class="dv">99</span> <span class="sc">~</span> <span class="st">"Too Slow"</span>,</span>
<span id="cb354-5"><a href="categorical-data-in-the-tidyverse.html#cb354-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">between</span>(hp, <span class="dv">100</span>, <span class="dv">199</span>) <span class="sc">~</span> <span class="st">"Fast"</span>,</span>
<span id="cb354-6"><a href="categorical-data-in-the-tidyverse.html#cb354-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">between</span>(hp, <span class="dv">200</span>, <span class="dv">299</span>) <span class="sc">~</span> <span class="st">"Very Fast"</span>,</span>
<span id="cb354-7"><a href="categorical-data-in-the-tidyverse.html#cb354-7" aria-hidden="true" tabindex="-1"></a> hp <span class="sc">></span> <span class="dv">300</span> <span class="sc">~</span> <span class="st">"Super Sonic"</span></span>
<span id="cb354-8"><a href="categorical-data-in-the-tidyverse.html#cb354-8" aria-hidden="true" tabindex="-1"></a> )) <span class="sc">%>%</span></span>
<span id="cb354-9"><a href="categorical-data-in-the-tidyverse.html#cb354-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(hp, speed)</span></code></pre></div>
<pre><code>## hp speed
## Mazda RX4 110 Fast
## Mazda RX4 Wag 110 Fast
## Datsun 710 93 Too Slow
## Hornet 4 Drive 110 Fast
## Hornet Sportabout 175 Fast
## Valiant 105 Fast
## Duster 360 245 Very Fast</code></pre>
</div>
<div id="case-study-on-flight-etiquette" class="section level2 hasAnchor" number="6.4">
<h2><span class="header-section-number">6.4</span> Case Study on Flight Etiquette<a href="categorical-data-in-the-tidyverse.html#case-study-on-flight-etiquette" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>Utilize the tools above to practice on an actual dataset:</p>
<div class="sourceCode" id="cb356"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb356-1"><a href="categorical-data-in-the-tidyverse.html#cb356-1" aria-hidden="true" tabindex="-1"></a>flying_etiquette <span class="ot"><-</span> <span class="fu">read_csv</span>(<span class="st">"https://assets.datacamp.com/production/repositories/1834/datasets/bef2c6e1ef42a2f230383e080fa7379912860017/flying-etiquette.csv"</span>)</span></code></pre></div>
<pre><code>## Rows: 1040 Columns: 27
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (26): How often do you travel by plane?, Do you ever recline your seat w...
## dbl (1): RespondentID
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.</code></pre>
<p><strong>Changing Characters To Factors</strong></p>
<p>There’re backticks around the column name because there are spaces in the column names! Normally, underscores are used.
Use <code>mutate_if()</code> to change <code>character</code> type columns into <code>factor</code> type:</p>
<div class="sourceCode" id="cb358"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb358-1"><a href="categorical-data-in-the-tidyverse.html#cb358-1" aria-hidden="true" tabindex="-1"></a>flying_etiquette <span class="sc">%>%</span></span>
<span id="cb358-2"><a href="categorical-data-in-the-tidyverse.html#cb358-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Change characters to factors</span></span>
<span id="cb358-3"><a href="categorical-data-in-the-tidyverse.html#cb358-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate_if</span>(is.character, as.factor) <span class="sc">%>%</span></span>
<span id="cb358-4"><a href="categorical-data-in-the-tidyverse.html#cb358-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Filter out those who have never flown on a plane</span></span>
<span id="cb358-5"><a href="categorical-data-in-the-tidyverse.html#cb358-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="st">`</span><span class="at">How often do you travel by plane?</span><span class="st">`</span> <span class="sc">!=</span> <span class="st">"Never"</span>)</span></code></pre></div>
<p><strong>Reformat/Filter Selected Data For Graphing</strong></p>
<p>Reformat the data to get it in shape for plotting. The graph only looks at flier’s opinions about whether different behaviors are rude, so select only those columns.</p>
<div class="sourceCode" id="cb359"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb359-1"><a href="categorical-data-in-the-tidyverse.html#cb359-1" aria-hidden="true" tabindex="-1"></a>gathered_data <span class="ot"><-</span> flying_etiquette <span class="sc">%>%</span></span>
<span id="cb359-2"><a href="categorical-data-in-the-tidyverse.html#cb359-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate_if</span>(is.character, as.factor) <span class="sc">%>%</span></span>
<span id="cb359-3"><a href="categorical-data-in-the-tidyverse.html#cb359-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="st">`</span><span class="at">How often do you travel by plane?</span><span class="st">`</span> <span class="sc">!=</span> <span class="st">"Never"</span>) <span class="sc">%>%</span></span>
<span id="cb359-4"><a href="categorical-data-in-the-tidyverse.html#cb359-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Select columns containing "rude"</span></span>
<span id="cb359-5"><a href="categorical-data-in-the-tidyverse.html#cb359-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(<span class="fu">contains</span>(<span class="st">"rude"</span>)) <span class="sc">%>%</span></span>
<span id="cb359-6"><a href="categorical-data-in-the-tidyverse.html#cb359-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Change format from wide to long</span></span>
<span id="cb359-7"><a href="categorical-data-in-the-tidyverse.html#cb359-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">gather</span>(response_var, value)</span></code></pre></div>
<pre><code>## Warning: attributes are not identical across measure variables;
## they will be dropped</code></pre>
<p><strong>Cleaning Up Strings</strong></p>
<p>Use <code>regex</code> or regular expressions to capture general patterns. For example, <code>.*science</code> would match <code>data science</code> in the string <code>data science rocks!</code>.</p>
<p>These are two basic notations:</p>
<pre><code>## Expression Does_This
## 1 . matches any character
## 2 * zero or more times</code></pre>
<div class="sourceCode" id="cb362"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb362-1"><a href="categorical-data-in-the-tidyverse.html#cb362-1" aria-hidden="true" tabindex="-1"></a>gathered_data[<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>,] <span class="sc">%>%</span></span>
<span id="cb362-2"><a href="categorical-data-in-the-tidyverse.html#cb362-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(response_var)</span></code></pre></div>
<pre><code>## # A tibble: 4 × 1
## response_var
## <chr>
## 1 Is itrude to move to an unsold seat on a plane?
## 2 Is itrude to move to an unsold seat on a plane?
## 3 Is itrude to move to an unsold seat on a plane?
## 4 Is itrude to move to an unsold seat on a plane?</code></pre>
<div class="sourceCode" id="cb364"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb364-1"><a href="categorical-data-in-the-tidyverse.html#cb364-1" aria-hidden="true" tabindex="-1"></a>gathered_data[<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>,] <span class="sc">%>%</span></span>
<span id="cb364-2"><a href="categorical-data-in-the-tidyverse.html#cb364-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove everything before and including "rude to " (with that space at the end!)</span></span>
<span id="cb364-3"><a href="categorical-data-in-the-tidyverse.html#cb364-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">response_var =</span> <span class="fu">str_remove</span>(response_var, <span class="st">".*rude to "</span>)) <span class="sc">%>%</span></span>
<span id="cb364-4"><a href="categorical-data-in-the-tidyverse.html#cb364-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove "on a plane"</span></span>
<span id="cb364-5"><a href="categorical-data-in-the-tidyverse.html#cb364-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">response_var =</span> <span class="fu">str_remove</span>(response_var, <span class="st">"on a plane"</span>))</span></code></pre></div>
<pre><code>## # A tibble: 4 × 2
## response_var value
## <chr> <chr>
## 1 move to an unsold seat ? <NA>
## 2 move to an unsold seat ? No, not rude at all
## 3 move to an unsold seat ? No, not rude at all
## 4 move to an unsold seat ? No, not rude at all</code></pre>
<p><strong>Dichotomizing Variables/Simplifying Observations</strong></p>
<p>In the <code>value</code> column, there are a variety of observations that indicates a range from <code>not rude</code>, <code>somwhat</code>, to <code>very rude</code>. To make it easier to plot, to simplify/categorize the observations, split it into 2 groups, <code>rude</code> and <code>not rude</code>.</p>
<div class="sourceCode" id="cb366"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb366-1"><a href="categorical-data-in-the-tidyverse.html#cb366-1" aria-hidden="true" tabindex="-1"></a>gathered_data[<span class="dv">1</span><span class="sc">:</span><span class="dv">10</span>,] <span class="sc">%>%</span></span>
<span id="cb366-2"><a href="categorical-data-in-the-tidyverse.html#cb366-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">pull</span>(value)</span></code></pre></div>
<pre><code>## [1] NA "No, not rude at all" "No, not rude at all"
## [4] "No, not rude at all" "No, not rude at all" "Yes, somewhat rude"
## [7] "No, not rude at all" "No, not rude at all" "No, not rude at all"
## [10] "Yes, very rude"</code></pre>
<p>Right now, our data is still in the format of one row per person per question. But for us to graph it, we’ll want to change that so each row is a question with the summary information about the response to that question.</p>
<div class="sourceCode" id="cb368"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb368-1"><a href="categorical-data-in-the-tidyverse.html#cb368-1" aria-hidden="true" tabindex="-1"></a>rude_behaviors <span class="ot"><-</span> gathered_data <span class="sc">%>%</span></span>
<span id="cb368-2"><a href="categorical-data-in-the-tidyverse.html#cb368-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">response_var =</span> <span class="fu">str_replace</span>(response_var, <span class="st">'.*rude to '</span>, <span class="st">''</span>)) <span class="sc">%>%</span></span>
<span id="cb368-3"><a href="categorical-data-in-the-tidyverse.html#cb368-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">response_var =</span> <span class="fu">str_replace</span>(response_var, <span class="st">'on a plane'</span>, <span class="st">''</span>)) <span class="sc">%>%</span></span>
<span id="cb368-4"><a href="categorical-data-in-the-tidyverse.html#cb368-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove rows that are NA in the value column</span></span>
<span id="cb368-5"><a href="categorical-data-in-the-tidyverse.html#cb368-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(value)) <span class="sc">%>%</span></span>
<span id="cb368-6"><a href="categorical-data-in-the-tidyverse.html#cb368-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">rude =</span> <span class="fu">if_else</span>(value <span class="sc">%in%</span> <span class="fu">c</span>(<span class="st">"No, not rude at all"</span>, <span class="st">"No, not at all rude"</span>), <span class="dv">0</span>, <span class="dv">1</span>)) <span class="sc">%>%</span></span>
<span id="cb368-7"><a href="categorical-data-in-the-tidyverse.html#cb368-7" aria-hidden="true" tabindex="-1"></a> <span class="co"># Group by response_var</span></span>
<span id="cb368-8"><a href="categorical-data-in-the-tidyverse.html#cb368-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(response_var) <span class="sc">%>%</span></span>
<span id="cb368-9"><a href="categorical-data-in-the-tidyverse.html#cb368-9" aria-hidden="true" tabindex="-1"></a> <span class="co"># Create perc_rude, the percent considering each behavior rude</span></span>
<span id="cb368-10"><a href="categorical-data-in-the-tidyverse.html#cb368-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarise</span>(<span class="at">perc_rude =</span> <span class="fu">mean</span>(rude))</span>
<span id="cb368-11"><a href="categorical-data-in-the-tidyverse.html#cb368-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb368-12"><a href="categorical-data-in-the-tidyverse.html#cb368-12" aria-hidden="true" tabindex="-1"></a>rude_behaviors</span></code></pre></div>
<pre><code>## # A tibble: 9 × 2
## response_var perc_rude
## <chr> <dbl>
## 1 ask someone to switch seats with you in order to be closer to famil… 0.171
## 2 ask someone to switch seats with you in order to be closer to frien… 0.258
## 3 bring a baby ? 0.303
## 4 knowingly bring unruly children ? 0.827
## 5 move to an unsold seat ? 0.193
## 6 recline your seat ? 0.412
## 7 say more than a few words tothe stranger sitting next to you ? 0.211
## 8 wake a passenger up if you are trying to go to the bathroom? 0.371
## 9 wake a passenger up if you are trying to walk around? 0.734</code></pre>
<p><strong>initial Plot</strong></p>
<p>The initial plot will have a few problems that are aesthetically unpleasing. For example, there are no labels, x-axis titles are jumbled up. We’ll fix it step by step:</p>
<div class="sourceCode" id="cb370"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb370-1"><a href="categorical-data-in-the-tidyverse.html#cb370-1" aria-hidden="true" tabindex="-1"></a>initial_plot <span class="ot"><-</span> rude_behaviors <span class="sc">%>%</span></span>
<span id="cb370-2"><a href="categorical-data-in-the-tidyverse.html#cb370-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># reorder response_var by perc_rude</span></span>
<span id="cb370-3"><a href="categorical-data-in-the-tidyverse.html#cb370-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">response_var =</span> <span class="fu">fct_reorder</span>(response_var, perc_rude)) <span class="sc">%>%</span></span>
<span id="cb370-4"><a href="categorical-data-in-the-tidyverse.html#cb370-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># make a bar plot of perc_rude by response_var</span></span>
<span id="cb370-5"><a href="categorical-data-in-the-tidyverse.html#cb370-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x =</span> response_var, <span class="at">y =</span> perc_rude)) <span class="sc">+</span> </span>
<span id="cb370-6"><a href="categorical-data-in-the-tidyverse.html#cb370-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_col</span>()</span>
<span id="cb370-7"><a href="categorical-data-in-the-tidyverse.html#cb370-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb370-8"><a href="categorical-data-in-the-tidyverse.html#cb370-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb370-9"><a href="categorical-data-in-the-tidyverse.html#cb370-9" aria-hidden="true" tabindex="-1"></a><span class="co"># View your plot</span></span>
<span id="cb370-10"><a href="categorical-data-in-the-tidyverse.html#cb370-10" aria-hidden="true" tabindex="-1"></a>initial_plot</span></code></pre></div>
<p><img src="06-Categorical_Data_in_Tidyverse_files/figure-html/unnamed-chunk-24-1.png" width="672" /></p>
<p><strong>Fixing Labels</strong></p>
<p>Our initial plot displays the data, but there’s a lot of work to do. Let’s start with the text above and around the plot. We’ve seen how the ggplot2 layer <code>labs()</code> can take multiple arguments to modify the text:</p>
<div class="sourceCode" id="cb371"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb371-1"><a href="categorical-data-in-the-tidyverse.html#cb371-1" aria-hidden="true" tabindex="-1"></a>titled_plot <span class="ot"><-</span> initial_plot <span class="sc">+</span> </span>
<span id="cb371-2"><a href="categorical-data-in-the-tidyverse.html#cb371-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Add the title, subtitle, and caption</span></span>
<span id="cb371-3"><a href="categorical-data-in-the-tidyverse.html#cb371-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Hell Is Other People In A Pressurized Metal Tube"</span>,</span>
<span id="cb371-4"><a href="categorical-data-in-the-tidyverse.html#cb371-4" aria-hidden="true" tabindex="-1"></a> <span class="at">subtitle =</span> <span class="st">"Percentage of 874 air-passenger respondents who said action is very or somewhat rude"</span>,</span>
<span id="cb371-5"><a href="categorical-data-in-the-tidyverse.html#cb371-5" aria-hidden="true" tabindex="-1"></a> <span class="at">caption =</span> <span class="st">"Source: SurveyMonkey Audience"</span>, </span>
<span id="cb371-6"><a href="categorical-data-in-the-tidyverse.html#cb371-6" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove the x- and y-axis labels</span></span>
<span id="cb371-7"><a href="categorical-data-in-the-tidyverse.html#cb371-7" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">""</span>,</span>
<span id="cb371-8"><a href="categorical-data-in-the-tidyverse.html#cb371-8" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">""</span>) </span>
<span id="cb371-9"><a href="categorical-data-in-the-tidyverse.html#cb371-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb371-10"><a href="categorical-data-in-the-tidyverse.html#cb371-10" aria-hidden="true" tabindex="-1"></a>titled_plot</span></code></pre></div>
<p><img src="06-Categorical_Data_in_Tidyverse_files/figure-html/unnamed-chunk-25-1.png" width="672" /></p>
<p><strong>Flipping X And Y For Better Visualization</strong></p>
<p>When the labels for our x-axis run into each other, we have two main choices: change the labels to be vertical instead of horizontal or flip the axes.</p>
<div class="sourceCode" id="cb372"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb372-1"><a href="categorical-data-in-the-tidyverse.html#cb372-1" aria-hidden="true" tabindex="-1"></a>flipped_plot <span class="ot"><-</span> titled_plot <span class="sc">+</span> </span>
<span id="cb372-2"><a href="categorical-data-in-the-tidyverse.html#cb372-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Flip the axes</span></span>
<span id="cb372-3"><a href="categorical-data-in-the-tidyverse.html#cb372-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">coord_flip</span>() <span class="sc">+</span> </span>
<span id="cb372-4"><a href="categorical-data-in-the-tidyverse.html#cb372-4" aria-hidden="true" tabindex="-1"></a> <span class="co"># Remove the x-axis ticks and labels</span></span>
<span id="cb372-5"><a href="categorical-data-in-the-tidyverse.html#cb372-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">axis.text.x =</span> <span class="fu">element_blank</span>(),</span>
<span id="cb372-6"><a href="categorical-data-in-the-tidyverse.html#cb372-6" aria-hidden="true" tabindex="-1"></a> <span class="at">axis.ticks.x =</span> <span class="fu">element_blank</span>())</span>
<span id="cb372-7"><a href="categorical-data-in-the-tidyverse.html#cb372-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb372-8"><a href="categorical-data-in-the-tidyverse.html#cb372-8" aria-hidden="true" tabindex="-1"></a>flipped_plot</span></code></pre></div>
<p><img src="06-Categorical_Data_in_Tidyverse_files/figure-html/unnamed-chunk-26-1.png" width="672" /></p>
<p><strong>Finalizing Steps</strong></p>
<p>The <code>FiveThirtyEight</code> chart displayed the bar’s height above each bar. We can do this with a <code>geom_text()</code> layer. Positioning the text just right above the bar takes a few extra argument.</p>
<p><code>position = position_dodge(0.9), vjust = 1</code> part of the code adjusts the percentage output position on the bar so it looks better.</p>
<div class="sourceCode" id="cb373"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb373-1"><a href="categorical-data-in-the-tidyverse.html#cb373-1" aria-hidden="true" tabindex="-1"></a>flipped_plot <span class="sc">+</span> </span>
<span id="cb373-2"><a href="categorical-data-in-the-tidyverse.html#cb373-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># Apply percent() to perc_rude to label above the bar with the perc value</span></span>
<span id="cb373-3"><a href="categorical-data-in-the-tidyverse.html#cb373-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_text</span>(<span class="fu">aes</span>(<span class="at">label =</span> <span class="fu">percent</span>(perc_rude), </span>
<span id="cb373-4"><a href="categorical-data-in-the-tidyverse.html#cb373-4" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> perc_rude <span class="sc">+</span> .<span class="dv">03</span>), </span>
<span id="cb373-5"><a href="categorical-data-in-the-tidyverse.html#cb373-5" aria-hidden="true" tabindex="-1"></a> <span class="at">position =</span> <span class="fu">position_dodge</span>(<span class="fl">0.9</span>),</span>
<span id="cb373-6"><a href="categorical-data-in-the-tidyverse.html#cb373-6" aria-hidden="true" tabindex="-1"></a> <span class="at">vjust =</span> <span class="dv">1</span>)</span></code></pre></div>
<p><img src="06-Categorical_Data_in_Tidyverse_files/figure-html/unnamed-chunk-27-1.png" width="672" /></p>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="working-with-data-in-the-tidyverse.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="data-manipulation-with-dplyr.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"whatsapp": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/alecng27/R_Programming_Guidebook/edit/master/06-Categorical_Data_in_Tidyverse.Rmd",
"text": "Edit"
},
"history": {
"link": "https://github.com/alecng27/R_Programming_Guidebook/commits/master/06-Categorical_Data_in_Tidyverse.Rmd",
"text": null
},
"view": {
"link": "https://github.com/alecng27/R_Programming_Guidebook/blob/master/06-Categorical_Data_in_Tidyverse.Rmd",
"text": null
},
"download": null,
"search": {
"engine": "fuse",
"options": null
},
"toc": {
"collapse": "section"
}
});
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
src = src.replace(/^https?:/, '');
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>