-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolutions.html
1572 lines (1513 loc) · 173 KB
/
solutions.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Solutions | R for PR evaluation</title>
<meta name="description" content="Theories, models, methods and objects of controlling - M.A. Seminar at the IfKW, SS 2022" />
<meta name="generator" content="bookdown 0.27 and GitBook 2.6.7" />
<meta property="og:title" content="Solutions | R for PR evaluation" />
<meta property="og:type" content="book" />
<meta property="og:description" content="Theories, models, methods and objects of controlling - M.A. Seminar at the IfKW, SS 2022" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Solutions | R for PR evaluation" />
<meta name="twitter:description" content="Theories, models, methods and objects of controlling - M.A. Seminar at the IfKW, SS 2022" />
<meta name="author" content="Lara Kobilke, IfKW, Ludwig-Maximilians-Universität München" />
<meta name="date" content="2022-07-20" />
<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="tidy-text-analysis.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><a href="./">Evaluation</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>General information on the course</a>
<ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#what-can-i-learn-from-this-tutorial"><i class="fa fa-check"></i>What can I learn from this tutorial?</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#what-can-i-do-if-i-have-a-question"><i class="fa fa-check"></i>What can I do if I have a question?</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#what-can-i-do-if-i-have-problems-with-my-r-code"><i class="fa fa-check"></i>What can I do if I have problems with my R code?</a></li>
</ul></li>
<li class="chapter" data-level="1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html"><i class="fa fa-check"></i><b>1</b> Tutorial: Installing & Understanding R/R Studio</a>
<ul>
<li class="chapter" data-level="1.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#installing-r"><i class="fa fa-check"></i><b>1.1</b> Installing R</a></li>
<li class="chapter" data-level="1.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#installing-r-studio"><i class="fa fa-check"></i><b>1.2</b> Installing R Studio</a></li>
<li class="chapter" data-level="1.3" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#updating-r-and-r-studio"><i class="fa fa-check"></i><b>1.3</b> Updating R and R Studio</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#on-windows"><i class="fa fa-check"></i><b>1.3.1</b> On Windows</a></li>
<li class="chapter" data-level="1.3.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#on-mac"><i class="fa fa-check"></i><b>1.3.2</b> On MAC</a></li>
</ul></li>
<li class="chapter" data-level="1.4" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#how-does-r-work"><i class="fa fa-check"></i><b>1.4</b> How does R work?</a></li>
<li class="chapter" data-level="1.5" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#why-should-i-use-r"><i class="fa fa-check"></i><b>1.5</b> Why should I use R?</a></li>
<li class="chapter" data-level="1.6" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#how-does-r-studio-work"><i class="fa fa-check"></i><b>1.6</b> How does R Studio work?</a>
<ul>
<li class="chapter" data-level="1.6.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#source-writing-your-own-code"><i class="fa fa-check"></i><b>1.6.1</b> Source: Writing your own code</a></li>
<li class="chapter" data-level="1.6.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#console-printing-results"><i class="fa fa-check"></i><b>1.6.2</b> Console: Printing results</a></li>
<li class="chapter" data-level="1.6.3" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#environment-overview-of-objects"><i class="fa fa-check"></i><b>1.6.3</b> Environment: Overview of objects</a></li>
<li class="chapter" data-level="1.6.4" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#plotshelppackages-do-everything-else"><i class="fa fa-check"></i><b>1.6.4</b> Plots/Help/Packages: Do everything else</a></li>
</ul></li>
<li class="chapter" data-level="1.7" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#packages"><i class="fa fa-check"></i><b>1.7</b> Packages</a>
<ul>
<li class="chapter" data-level="1.7.1" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#installing-packages"><i class="fa fa-check"></i><b>1.7.1</b> Installing packages</a></li>
<li class="chapter" data-level="1.7.2" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#activating-packages"><i class="fa fa-check"></i><b>1.7.2</b> Activating packages</a></li>
<li class="chapter" data-level="1.7.3" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#getting-information-about-packages"><i class="fa fa-check"></i><b>1.7.3</b> Getting information about packages</a></li>
</ul></li>
<li class="chapter" data-level="1.8" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#take-aways"><i class="fa fa-check"></i><b>1.8</b> Take-Aways</a></li>
<li class="chapter" data-level="1.9" data-path="tutorial-installing-understanding-rr-studio.html"><a href="tutorial-installing-understanding-rr-studio.html#additional-tutorials"><i class="fa fa-check"></i><b>1.9</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html"><i class="fa fa-check"></i><b>2</b> Tutorial: Using R as a calculator</a>
<ul>
<li class="chapter" data-level="2.1" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#using-variables-for-calculation"><i class="fa fa-check"></i><b>2.1</b> Using variables for calculation</a></li>
<li class="chapter" data-level="2.2" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#using-vectors-for-calculation"><i class="fa fa-check"></i><b>2.2</b> Using vectors for calculation</a></li>
<li class="chapter" data-level="2.3" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#selecting-values-from-a-vector"><i class="fa fa-check"></i><b>2.3</b> Selecting values from a vector</a></li>
<li class="chapter" data-level="2.4" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#take-aways-1"><i class="fa fa-check"></i><b>2.4</b> Take-Aways</a></li>
<li class="chapter" data-level="2.5" data-path="tutorial-using-r-as-a-calculator.html"><a href="tutorial-using-r-as-a-calculator.html#additional-tutorials-1"><i class="fa fa-check"></i><b>2.5</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html"><i class="fa fa-check"></i><b>3</b> Tutorial: Working with data (files)</a>
<ul>
<li class="chapter" data-level="3.1" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#defining-your-working-directory"><i class="fa fa-check"></i><b>3.1</b> Defining your working directory</a>
<ul>
<li class="chapter" data-level="3.1.1" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#optional-setting-the-working-directory-on-a-remote-desktop"><i class="fa fa-check"></i><b>3.1.1</b> Optional: Setting the working directory on a remote desktop</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#import-data-from-your-working-directory"><i class="fa fa-check"></i><b>3.2</b> Import data from your working directory</a></li>
<li class="chapter" data-level="3.3" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-variables-columns-in-data-frames"><i class="fa fa-check"></i><b>3.3</b> Subsetting variables / columns in data frames</a></li>
<li class="chapter" data-level="3.4" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-observations-rows-in-data-frames"><i class="fa fa-check"></i><b>3.4</b> Subsetting observations / rows in data frames</a></li>
<li class="chapter" data-level="3.5" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-values-cells-in-data-frames"><i class="fa fa-check"></i><b>3.5</b> Subsetting values / cells in data frames</a></li>
<li class="chapter" data-level="3.6" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#subsetting-data-with-conditions"><i class="fa fa-check"></i><b>3.6</b> Subsetting data with conditions</a></li>
<li class="chapter" data-level="3.7" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#take-aways-2"><i class="fa fa-check"></i><b>3.7</b> Take-Aways</a></li>
<li class="chapter" data-level="3.8" data-path="tutorial-working-with-data-files.html"><a href="tutorial-working-with-data-files.html#additional-tutorials-2"><i class="fa fa-check"></i><b>3.8</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html"><i class="fa fa-check"></i>Exercise 1</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-1"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-2"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-3"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-4"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-5"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-1.html"><a href="exercise-1.html#task-6"><i class="fa fa-check"></i>Task 6</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html"><i class="fa fa-check"></i><b>4</b> Tutorial: Data management with tidyverse</a>
<ul>
<li class="chapter" data-level="4.1" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#why-not-stick-with-base-r"><i class="fa fa-check"></i><b>4.1</b> Why not stick with Base R?</a></li>
<li class="chapter" data-level="4.2" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#tidyverse-packages"><i class="fa fa-check"></i><b>4.2</b> Tidyverse packages</a></li>
<li class="chapter" data-level="4.3" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#tidy-data"><i class="fa fa-check"></i><b>4.3</b> Tidy data</a></li>
<li class="chapter" data-level="4.4" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#the-pipe-operator"><i class="fa fa-check"></i><b>4.4</b> The pipe operator</a></li>
<li class="chapter" data-level="4.5" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#data-transformation-with-dplyr"><i class="fa fa-check"></i><b>4.5</b> Data transformation with dplyr</a>
<ul>
<li class="chapter" data-level="4.5.1" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#select"><i class="fa fa-check"></i><b>4.5.1</b> select()</a></li>
<li class="chapter" data-level="4.5.2" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#filter"><i class="fa fa-check"></i><b>4.5.2</b> filter()</a></li>
<li class="chapter" data-level="4.5.3" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#arrange"><i class="fa fa-check"></i><b>4.5.3</b> arrange()</a></li>
<li class="chapter" data-level="4.5.4" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#mutate"><i class="fa fa-check"></i><b>4.5.4</b> mutate()</a></li>
<li class="chapter" data-level="4.5.5" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#summarize-group_by"><i class="fa fa-check"></i><b>4.5.5</b> summarize() [+ group_by()]</a></li>
<li class="chapter" data-level="4.5.6" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#chaining-functions-in-a-pipe"><i class="fa fa-check"></i><b>4.5.6</b> Chaining functions in a pipe</a></li>
</ul></li>
<li class="chapter" data-level="4.6" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#take-aways-3"><i class="fa fa-check"></i><b>4.6</b> Take-Aways</a></li>
<li class="chapter" data-level="4.7" data-path="tutorial-data-management-with-tidyverse.html"><a href="tutorial-data-management-with-tidyverse.html#additional-tutorials-3"><i class="fa fa-check"></i><b>4.7</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 2: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-1-1"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-2-1"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-3-1"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-4-1"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-5-1"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-6-1"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="exercise-2-test-your-knowledge.html"><a href="exercise-2-test-your-knowledge.html#task-7"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html"><i class="fa fa-check"></i><b>5</b> Tutorial: Data visualization with ggplot</a>
<ul>
<li class="chapter" data-level="5.1" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#why-not-stick-with-base-r-1"><i class="fa fa-check"></i><b>5.1</b> Why not stick with Base R?</a></li>
<li class="chapter" data-level="5.2" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#components-of-a-ggplot-graph"><i class="fa fa-check"></i><b>5.2</b> Components of a ggplot graph</a></li>
<li class="chapter" data-level="5.3" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#installing-activating-ggplot"><i class="fa fa-check"></i><b>5.3</b> Installing & activating ggplot</a></li>
<li class="chapter" data-level="5.4" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#building-your-first-plot"><i class="fa fa-check"></i><b>5.4</b> Building your first plot</a>
<ul>
<li class="chapter" data-level="5.4.1" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#data"><i class="fa fa-check"></i><b>5.4.1</b> Data</a></li>
<li class="chapter" data-level="5.4.2" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#aesthetics"><i class="fa fa-check"></i><b>5.4.2</b> Aesthetics</a></li>
<li class="chapter" data-level="5.4.3" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#geometrics"><i class="fa fa-check"></i><b>5.4.3</b> Geometrics</a></li>
<li class="chapter" data-level="5.4.4" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#scales"><i class="fa fa-check"></i><b>5.4.4</b> Scales</a></li>
<li class="chapter" data-level="5.4.5" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#themes"><i class="fa fa-check"></i><b>5.4.5</b> Themes</a></li>
<li class="chapter" data-level="5.4.6" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#labs"><i class="fa fa-check"></i><b>5.4.6</b> Labs</a></li>
<li class="chapter" data-level="5.4.7" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#facets"><i class="fa fa-check"></i><b>5.4.7</b> Facets</a></li>
<li class="chapter" data-level="5.4.8" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#saving-graphs"><i class="fa fa-check"></i><b>5.4.8</b> Saving graphs</a></li>
</ul></li>
<li class="chapter" data-level="5.5" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#other-common-plot-types"><i class="fa fa-check"></i><b>5.5</b> Other common plot types</a>
<ul>
<li class="chapter" data-level="5.5.1" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#bar-plots"><i class="fa fa-check"></i><b>5.5.1</b> bar plots</a></li>
<li class="chapter" data-level="5.5.2" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#box-plots"><i class="fa fa-check"></i><b>5.5.2</b> box plots</a></li>
</ul></li>
<li class="chapter" data-level="5.6" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#take-aways-4"><i class="fa fa-check"></i><b>5.6</b> Take Aways</a></li>
<li class="chapter" data-level="5.7" data-path="tutorial-data-visualization-with-ggplot.html"><a href="tutorial-data-visualization-with-ggplot.html#additional-tutorials-4"><i class="fa fa-check"></i><b>5.7</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 3: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html#task-1-2"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html#task-2-2"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-3-test-your-knowledge.html"><a href="exercise-3-test-your-knowledge.html#task-3-2"><i class="fa fa-check"></i>Task 3</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 4: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-1-3"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-2-3"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-3-3"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-4-2"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-5-2"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-6-2"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-7-1"><i class="fa fa-check"></i>Task 7</a></li>
<li class="chapter" data-level="" data-path="exercise-4-test-your-knowledge.html"><a href="exercise-4-test-your-knowledge.html#task-8"><i class="fa fa-check"></i>Task 8</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html"><i class="fa fa-check"></i><b>6</b> Tutorial: Text manipulation with stringr</a>
<ul>
<li class="chapter" data-level="6.1" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#whats-stringr"><i class="fa fa-check"></i><b>6.1</b> What’s stringr?</a></li>
<li class="chapter" data-level="6.2" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#working-with-strings"><i class="fa fa-check"></i><b>6.2</b> Working with strings</a></li>
<li class="chapter" data-level="6.3" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#working-with-string-patterns"><i class="fa fa-check"></i><b>6.3</b> Working with string patterns</a></li>
<li class="chapter" data-level="6.4" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#working-with-regular-expressions"><i class="fa fa-check"></i><b>6.4</b> Working with regular expressions</a></li>
<li class="chapter" data-level="6.5" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#take-aways-5"><i class="fa fa-check"></i><b>6.5</b> Take-Aways</a></li>
<li class="chapter" data-level="6.6" data-path="tutorial-text-manipulation-with-stringr.html"><a href="tutorial-text-manipulation-with-stringr.html#additional-tutorials-5"><i class="fa fa-check"></i><b>6.6</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html"><i class="fa fa-check"></i>Exercise 5: Test your knowledge</a>
<ul>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-1-4"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-2-4"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-3-4"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-4-3"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-5-3"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-6-3"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="exercise-5-test-your-knowledge.html"><a href="exercise-5-test-your-knowledge.html#task-7-2"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html"><i class="fa fa-check"></i><b>7</b> Tidy text analysis</a>
<ul>
<li class="chapter" data-level="7.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#what-is-tidy-text"><i class="fa fa-check"></i><b>7.1</b> What is tidy text?</a></li>
<li class="chapter" data-level="7.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#preprocessing-real-world-data"><i class="fa fa-check"></i><b>7.2</b> Preprocessing real-world data</a>
<ul>
<li class="chapter" data-level="7.2.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#tokenization"><i class="fa fa-check"></i><b>7.2.1</b> Tokenization</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-tokenization"><i class="fa fa-check"></i>Practice: Tokenization</a></li>
<li class="chapter" data-level="7.2.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#stop-word-removal"><i class="fa fa-check"></i><b>7.2.2</b> Stop word removal</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-stop-word-removal"><i class="fa fa-check"></i>Practice: Stop word removal</a></li>
<li class="chapter" data-level="7.2.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#lemmatizing-stemming"><i class="fa fa-check"></i><b>7.2.3</b> Lemmatizing & stemming</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-lemmatizing-stemming"><i class="fa fa-check"></i>Practice: Lemmatizing & stemming</a></li>
<li class="chapter" data-level="7.2.4" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#pruning"><i class="fa fa-check"></i><b>7.2.4</b> Pruning</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-pruning"><i class="fa fa-check"></i>Practice: Pruning</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#relative-word-frequencies"><i class="fa fa-check"></i><b>7.3</b> (Relative) word frequencies</a>
<ul>
<li class="chapter" data-level="7.3.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#advanced-word-frequency-comparisons"><i class="fa fa-check"></i><b>7.3.1</b> Advanced: Word frequency comparisons</a></li>
<li class="chapter" data-level="7.3.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#advanced-word-log-odds"><i class="fa fa-check"></i><b>7.3.2</b> Advanced: Word log odds</a></li>
</ul></li>
<li class="chapter" data-level="7.4" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#topic-modeling"><i class="fa fa-check"></i><b>7.4</b> Topic modeling</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#first-steps"><i class="fa fa-check"></i><b>7.4.1</b> First steps</a></li>
<li class="chapter" data-level="7.4.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#model-estimation-number-of-topics"><i class="fa fa-check"></i><b>7.4.2</b> Model estimation: Number of topics</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-model-estimation"><i class="fa fa-check"></i>Practice: Model estimation</a></li>
<li class="chapter" data-level="7.4.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#inspect-the-topics"><i class="fa fa-check"></i><b>7.4.3</b> Inspect the topics</a></li>
<li class="chapter" data-level="7.4.4" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#stms"><i class="fa fa-check"></i><b>7.4.4</b> STMs</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-analysis"><i class="fa fa-check"></i><b>7.5</b> Sentiment analysis</a>
<ul>
<li class="chapter" data-level="7.5.1" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-over-time"><i class="fa fa-check"></i><b>7.5.1</b> Sentiment over time</a></li>
<li class="chapter" data-level="" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#practice-sentiment-over-time"><i class="fa fa-check"></i>Practice: Sentiment over time</a></li>
<li class="chapter" data-level="7.5.2" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-over-time-periods"><i class="fa fa-check"></i><b>7.5.2</b> Sentiment over time periods</a></li>
<li class="chapter" data-level="7.5.3" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#sentiment-of-individual-users"><i class="fa fa-check"></i><b>7.5.3</b> Sentiment of individual users</a></li>
</ul></li>
<li class="chapter" data-level="7.6" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#take-aways-6"><i class="fa fa-check"></i><b>7.6</b> Take-Aways</a></li>
<li class="chapter" data-level="7.7" data-path="tidy-text-analysis.html"><a href="tidy-text-analysis.html#additional-tutorials-6"><i class="fa fa-check"></i><b>7.7</b> Additional tutorials</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html"><i class="fa fa-check"></i>Solutions</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-1"><i class="fa fa-check"></i>Solutions for Exercise 1</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-5"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-5"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-5"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-4"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-4"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-4"><i class="fa fa-check"></i>Task 6</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-2"><i class="fa fa-check"></i>Solutions for Exercise 2</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-6"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-6"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-6"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-5"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-5"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-5"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-3"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-3"><i class="fa fa-check"></i>Solutions for Exercise 3</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-7"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-7"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-7"><i class="fa fa-check"></i>Task 3</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-4"><i class="fa fa-check"></i>Solutions for Exercise 4</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-8"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-8"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-8"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-6"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-6"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-6"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-4"><i class="fa fa-check"></i>Task 7</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-8-1"><i class="fa fa-check"></i>Task 8</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-5"><i class="fa fa-check"></i>Solutions for Exercise 5</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-9"><i class="fa fa-check"></i>Task 1</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-9"><i class="fa fa-check"></i>Task 2</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-9"><i class="fa fa-check"></i>Task 3</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-7"><i class="fa fa-check"></i>Task 4</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-7"><i class="fa fa-check"></i>Task 5</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-7"><i class="fa fa-check"></i>Task 6</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-5"><i class="fa fa-check"></i>Task 7</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#solutions-for-exercise-6"><i class="fa fa-check"></i>Solutions for Exercise 6</a>
<ul>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-1-tokenization"><i class="fa fa-check"></i>Task 1: Tokenization</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-2-stop-word-removal"><i class="fa fa-check"></i>Task 2: Stop word removal</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-3-lemmatizing-stemming"><i class="fa fa-check"></i>Task 3: Lemmatizing & stemming</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-4-pruning"><i class="fa fa-check"></i>Task 4: Pruning</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-5-model-estimation"><i class="fa fa-check"></i>Task 5: Model estimation</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-6-word-topic-probabilities"><i class="fa fa-check"></i>Task 6: Word-topic probabilities</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-7-document-topic-probabilities"><i class="fa fa-check"></i>Task 7: Document-topic probabilities</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-8-sentiment-over-time"><i class="fa fa-check"></i>Task 8: Sentiment over time</a></li>
<li class="chapter" data-level="" data-path="solutions.html"><a href="solutions.html#task-9-sentiment-over-time-periods"><i class="fa fa-check"></i>Task 9: Sentiment over time periods</a></li>
</ul></li>
</ul></li>
<li class="divider"></li>
<li><a href="https://github.com/LKobilke/PR-Erfolgskontrolle" target="blank">Published with bookdown</a></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 for PR evaluation</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="solutions" class="section level1 unnumbered hasAnchor">
<h1>Solutions<a href="solutions.html#solutions" class="anchor-section" aria-label="Anchor link to header"></a></h1>
<p>This is where you’ll find solutions for all of the tutorials.</p>
<div id="solutions-for-exercise-1" class="section level2 unnumbered hasAnchor">
<h2>Solutions for Exercise 1<a href="solutions.html#solutions-for-exercise-1" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="task-1-5" class="section level3 unnumbered hasAnchor">
<h3>Task 1<a href="solutions.html#task-1-5" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Below you will see multiple choice questions. Please try to identify the
correct answers. 1, 2, 3 and 4 correct answers are possible for each
question.</p>
<p><strong>1. What panels are part of RStudio?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li>source (x)</li>
<li>console (x)</li>
<li>packages, files & plots (x)</li>
</ul>
<p><strong>2. How do you activate R packages after you have installed them?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li>library() (x)</li>
</ul>
<p><strong>3. How do you create a vector in R with elements 1, 2, 3?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li>c(1,2,3) (x)</li>
</ul>
<p><strong>4. Imagine you have a vector called ‘vector’ with 10 numeric elements.
How do you retrieve the 8th element?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li>vector[8] (x)</li>
</ul>
<p><strong>5. Imagine you have a vector called ‘hair’ with 5 elements: brown,
black, red, blond, other. How do you retrieve the color ‘blond’?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li>hair[4] (x)</li>
</ul>
</div>
<div id="task-2-5" class="section level3 unnumbered hasAnchor">
<h3>Task 2<a href="solutions.html#task-2-5" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Create a numeric vector with 8 values and assign the name <em>age</em> to the
vector. First, display all elements of the vector. Then print only the
5th element. After that, display all elements except the 5th. Finally,
display the elements at the positions 6 to 8.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb302"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb302-1"><a href="solutions.html#cb302-1" aria-hidden="true" tabindex="-1"></a>age <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">65</span>,<span class="dv">52</span>,<span class="dv">73</span>,<span class="dv">71</span>,<span class="dv">80</span>,<span class="dv">62</span>,<span class="dv">68</span>,<span class="dv">87</span>)</span>
<span id="cb302-2"><a href="solutions.html#cb302-2" aria-hidden="true" tabindex="-1"></a>age</span></code></pre></div>
<pre><code>## [1] 65 52 73 71 80 62 68 87</code></pre>
<div class="sourceCode" id="cb304"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb304-1"><a href="solutions.html#cb304-1" aria-hidden="true" tabindex="-1"></a>age[<span class="dv">5</span>]</span></code></pre></div>
<pre><code>## [1] 80</code></pre>
<div class="sourceCode" id="cb306"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb306-1"><a href="solutions.html#cb306-1" aria-hidden="true" tabindex="-1"></a>age[<span class="sc">-</span><span class="dv">5</span>]</span></code></pre></div>
<pre><code>## [1] 65 52 73 71 62 68 87</code></pre>
<div class="sourceCode" id="cb308"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb308-1"><a href="solutions.html#cb308-1" aria-hidden="true" tabindex="-1"></a>age[<span class="dv">6</span><span class="sc">:</span><span class="dv">8</span>]</span></code></pre></div>
<pre><code>## [1] 62 68 87</code></pre>
</div>
<div id="task-3-5" class="section level3 unnumbered hasAnchor">
<h3>Task 3<a href="solutions.html#task-3-5" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Create a non-numeric, i.e. character, vector with 4 elements and assign
the name <em>eye_color</em> to the vector. First, print all elements of this
vector to the console. Then have only the value in the 2nd element
displayed, then all values except the 2nd element. At the end, display
the elements at the positions 2 to 4.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb310"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb310-1"><a href="solutions.html#cb310-1" aria-hidden="true" tabindex="-1"></a>eye_color <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"blue"</span>, <span class="st">"green"</span>, <span class="st">"brown"</span>, <span class="st">"other"</span>)</span>
<span id="cb310-2"><a href="solutions.html#cb310-2" aria-hidden="true" tabindex="-1"></a>eye_color</span></code></pre></div>
<pre><code>## [1] "blue" "green" "brown" "other"</code></pre>
<div class="sourceCode" id="cb312"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb312-1"><a href="solutions.html#cb312-1" aria-hidden="true" tabindex="-1"></a>eye_color[<span class="dv">2</span>]</span></code></pre></div>
<pre><code>## [1] "green"</code></pre>
<div class="sourceCode" id="cb314"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb314-1"><a href="solutions.html#cb314-1" aria-hidden="true" tabindex="-1"></a>eye_color[<span class="sc">-</span><span class="dv">2</span>]</span></code></pre></div>
<pre><code>## [1] "blue" "brown" "other"</code></pre>
<div class="sourceCode" id="cb316"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb316-1"><a href="solutions.html#cb316-1" aria-hidden="true" tabindex="-1"></a>eye_color[<span class="dv">2</span><span class="sc">:</span><span class="dv">4</span>]</span></code></pre></div>
<pre><code>## [1] "green" "brown" "other"</code></pre>
</div>
<div id="task-4-4" class="section level3 unnumbered hasAnchor">
<h3>Task 4<a href="solutions.html#task-4-4" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Get the <em>“data_tutorial2.csv”</em> from Moodle ( <em>4. Mai material folder</em> )
and put it into the folder that you want to use as working directory.</p>
<p>Set your working directory and load the data into R by saving it into a
source object called <em>data</em>. <strong>Note:</strong> This time, it’s a csv that is
actually separated by commas, not by semicolons.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb318"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb318-1"><a href="solutions.html#cb318-1" aria-hidden="true" tabindex="-1"></a><span class="fu">setwd</span>(<span class="st">"C:/Users/LaraK/Documents/IPR/"</span>)</span>
<span id="cb318-2"><a href="solutions.html#cb318-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">read.csv</span>(<span class="st">"data_tutorial2.csv"</span>, <span class="at">header =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
</div>
<div id="task-5-4" class="section level3 unnumbered hasAnchor">
<h3>Task 5<a href="solutions.html#task-5-4" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Now, print only the age column to the console. Use the <code>$</code> operator
first. Then try to achieve the same result using the subsetting
operators, i.e. <code>[]</code>.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb319"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb319-1"><a href="solutions.html#cb319-1" aria-hidden="true" tabindex="-1"></a>data<span class="sc">$</span>age <span class="co"># first version</span></span></code></pre></div>
<pre><code>## [1] 20 25 29 22 25 26 26 27 8 26 27 26 25 27 29 26 21 23 24 26</code></pre>
<div class="sourceCode" id="cb321"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb321-1"><a href="solutions.html#cb321-1" aria-hidden="true" tabindex="-1"></a>data[,<span class="dv">2</span>] <span class="co"># second version</span></span></code></pre></div>
<pre><code>## [1] 20 25 29 22 25 26 26 27 8 26 27 26 25 27 29 26 21 23 24 26</code></pre>
</div>
<div id="task-6-4" class="section level3 unnumbered hasAnchor">
<h3>Task 6<a href="solutions.html#task-6-4" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Print only the first 6 age numbers to the console. Use the <code>$</code> operator
first. Then try to achieve the same result using the subsetting
operators, i.e. <code>[]</code>.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb323"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb323-1"><a href="solutions.html#cb323-1" aria-hidden="true" tabindex="-1"></a>data<span class="sc">$</span>age[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>] <span class="co"># first version</span></span></code></pre></div>
<pre><code>## [1] 20 25 29 22 25 26</code></pre>
<div class="sourceCode" id="cb325"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb325-1"><a href="solutions.html#cb325-1" aria-hidden="true" tabindex="-1"></a>data[<span class="dv">1</span><span class="sc">:</span><span class="dv">6</span>,<span class="dv">2</span>] <span class="co"># second version</span></span></code></pre></div>
<pre><code>## [1] 20 25 29 22 25 26</code></pre>
</div>
</div>
<div id="solutions-for-exercise-2" class="section level2 unnumbered hasAnchor">
<h2>Solutions for Exercise 2<a href="solutions.html#solutions-for-exercise-2" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="task-1-6" class="section level3 unnumbered hasAnchor">
<h3>Task 1<a href="solutions.html#task-1-6" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Below you will see multiple choice questions. Please try to identify the
correct answers. 1, 2, 3 and 4 correct answers are possible for each
question.</p>
<p><strong>1. What are the main characteristics of tidy data?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li>Every observation is a row. (x)</li>
</ul>
<p><strong>2. What are <code>dplyr</code> functions?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li><code>mutate()</code> (x)</li>
</ul>
<p><strong>3. How can you sort the eye_color of Star Wars characters from Z to
A?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li><code>starwars_data %>% arrange(desc(eye_color))</code> (x)</li>
<li><code>starwars_data %>% select(eye_color) %>% arrange(desc(eye_color))</code>
<ol start="24" style="list-style-type: lower-alpha">
<li></li>
</ol></li>
</ul>
<p><strong>4. Imagine you want to recode the height of the these characters. You
want to have three categories from small and medium to tall. What is a
valid approach?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li><code>starwars_data %>% mutate(height = case_when(height<=150~"small",height<=190~"medium",height>190~"tall"))</code>
<ol start="24" style="list-style-type: lower-alpha">
<li></li>
</ol></li>
</ul>
<p><strong>5. Imagine you want to provide a systematic overview over all hair
colors and what species wear these hair colors frequently (not
accounting for the skewed sampling of species)? What is a valid
approach?</strong></p>
<p><em>Solution:</em></p>
<ul>
<li><code>starwars_data %>% group_by(hair_color, species) %>% summarize(count = n()) %>% arrange(hair_color)</code>
<ol start="24" style="list-style-type: lower-alpha">
<li></li>
</ol></li>
</ul>
</div>
<div id="task-2-6" class="section level3 unnumbered hasAnchor">
<h3>Task 2<a href="solutions.html#task-2-6" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Now it’s you turn. Load the starwars data like this:</p>
<div class="sourceCode" id="cb327"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb327-1"><a href="solutions.html#cb327-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(dplyr) <span class="co"># to activate the dplyr package</span></span>
<span id="cb327-2"><a href="solutions.html#cb327-2" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="ot"><-</span> starwars <span class="co"># to assign the pre-installed starwars data set (dplyr) into a source object in our environment</span></span></code></pre></div>
<p>How many humans are contained in the starwars data overall? (Hint: use
<code>summarize(count = n())</code> or <code>count()</code>)?</p>
<p><em>Solution:</em></p>
<p>You can use <code>summarize(count = n())</code>:</p>
<div class="sourceCode" id="cb328"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb328-1"><a href="solutions.html#cb328-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb328-2"><a href="solutions.html#cb328-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">==</span> <span class="st">"Human"</span>) <span class="sc">%>%</span> </span>
<span id="cb328-3"><a href="solutions.html#cb328-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">count =</span> <span class="fu">n</span>())</span></code></pre></div>
<pre><code>## # A tibble: 1 × 1
## count
## <int>
## 1 35</code></pre>
<p>Alternatively, you can use the <code>count()</code> function:</p>
<div class="sourceCode" id="cb330"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb330-1"><a href="solutions.html#cb330-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb330-2"><a href="solutions.html#cb330-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">==</span> <span class="st">"Human"</span>) <span class="sc">%>%</span> </span>
<span id="cb330-3"><a href="solutions.html#cb330-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(species)</span></code></pre></div>
<pre><code>## # A tibble: 1 × 2
## species n
## <chr> <int>
## 1 Human 35</code></pre>
</div>
<div id="task-3-6" class="section level3 unnumbered hasAnchor">
<h3>Task 3<a href="solutions.html#task-3-6" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>How many humans are contained in starwars by gender?</p>
<p><em>Solution:</em></p>
<p>You can use <code>summarize(count = n())</code>:</p>
<div class="sourceCode" id="cb332"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb332-1"><a href="solutions.html#cb332-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb332-2"><a href="solutions.html#cb332-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">==</span> <span class="st">"Human"</span>) <span class="sc">%>%</span> </span>
<span id="cb332-3"><a href="solutions.html#cb332-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(species, gender) <span class="sc">%>%</span> </span>
<span id="cb332-4"><a href="solutions.html#cb332-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">count =</span> <span class="fu">n</span>())</span></code></pre></div>
<pre><code>## # A tibble: 2 × 3
## # Groups: species [1]
## species gender count
## <chr> <chr> <int>
## 1 Human feminine 9
## 2 Human masculine 26</code></pre>
<p>Alternatively, you can use the <code>count()</code> function:</p>
<div class="sourceCode" id="cb334"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb334-1"><a href="solutions.html#cb334-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb334-2"><a href="solutions.html#cb334-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">==</span> <span class="st">"Human"</span>) <span class="sc">%>%</span> </span>
<span id="cb334-3"><a href="solutions.html#cb334-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(species, gender)</span></code></pre></div>
<pre><code>## # A tibble: 2 × 3
## species gender n
## <chr> <chr> <int>
## 1 Human feminine 9
## 2 Human masculine 26</code></pre>
</div>
<div id="task-4-5" class="section level3 unnumbered hasAnchor">
<h3>Task 4<a href="solutions.html#task-4-5" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>What is the most common eye_color among Star Wars characters? (Hint: use
<code>arrange()</code>)__</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb336"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb336-1"><a href="solutions.html#cb336-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb336-2"><a href="solutions.html#cb336-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(eye_color) <span class="sc">%>%</span> </span>
<span id="cb336-3"><a href="solutions.html#cb336-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">count =</span> <span class="fu">n</span>()) <span class="sc">%>%</span></span>
<span id="cb336-4"><a href="solutions.html#cb336-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(count))</span></code></pre></div>
<pre><code>## # A tibble: 15 × 2
## eye_color count
## <chr> <int>
## 1 brown 21
## 2 blue 19
## 3 yellow 11
## 4 black 10
## 5 orange 8
## 6 red 5
## 7 hazel 3
## 8 unknown 3
## 9 blue-gray 1
## 10 dark 1
## 11 gold 1
## 12 green, yellow 1
## 13 pink 1
## 14 red, blue 1
## 15 white 1</code></pre>
</div>
<div id="task-5-5" class="section level3 unnumbered hasAnchor">
<h3>Task 5<a href="solutions.html#task-5-5" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>What is the average mass of Star Wars characters that are not human and
have yellow eyes? (Hint: remove all <code>NAs</code>)__</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb338"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb338-1"><a href="solutions.html#cb338-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb338-2"><a href="solutions.html#cb338-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species <span class="sc">!=</span> <span class="st">"Human"</span> <span class="sc">&</span> eye_color<span class="sc">==</span><span class="st">"yellow"</span>) <span class="sc">%>%</span></span>
<span id="cb338-3"><a href="solutions.html#cb338-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">mean_mass =</span> <span class="fu">mean</span>(mass, <span class="at">na.rm=</span><span class="cn">TRUE</span>))</span></code></pre></div>
<pre><code>## # A tibble: 1 × 1
## mean_mass
## <dbl>
## 1 74.1</code></pre>
</div>
<div id="task-6-5" class="section level3 unnumbered hasAnchor">
<h3>Task 6<a href="solutions.html#task-6-5" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Compare the mean, median, and standard deviation of mass for all humans
and droids. (Hint: remove all <code>NAs</code>)__</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb340"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb340-1"><a href="solutions.html#cb340-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span></span>
<span id="cb340-2"><a href="solutions.html#cb340-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(species<span class="sc">==</span><span class="st">"Human"</span> <span class="sc">|</span> species<span class="sc">==</span><span class="st">"Droid"</span>) <span class="sc">%>%</span></span>
<span id="cb340-3"><a href="solutions.html#cb340-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(species) <span class="sc">%>%</span> </span>
<span id="cb340-4"><a href="solutions.html#cb340-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">M =</span> <span class="fu">mean</span>(mass, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb340-5"><a href="solutions.html#cb340-5" aria-hidden="true" tabindex="-1"></a> <span class="at">Med =</span> <span class="fu">median</span>(mass, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb340-6"><a href="solutions.html#cb340-6" aria-hidden="true" tabindex="-1"></a> <span class="at">SD =</span> <span class="fu">sd</span>(mass, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb340-7"><a href="solutions.html#cb340-7" aria-hidden="true" tabindex="-1"></a> )</span></code></pre></div>
<pre><code>## # A tibble: 2 × 4
## species M Med SD
## <chr> <dbl> <dbl> <dbl>
## 1 Droid 69.8 53.5 51.0
## 2 Human 82.8 79 19.4</code></pre>
</div>
<div id="task-7-3" class="section level3 unnumbered hasAnchor">
<h3>Task 7<a href="solutions.html#task-7-3" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Create a new variable in which you store the mass in gram. Add it to the
data frame.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb342"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb342-1"><a href="solutions.html#cb342-1" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="ot"><-</span> starwars_data <span class="sc">%>%</span></span>
<span id="cb342-2"><a href="solutions.html#cb342-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">gr_mass =</span> mass<span class="sc">*</span><span class="dv">1000</span>)</span>
<span id="cb342-3"><a href="solutions.html#cb342-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb342-4"><a href="solutions.html#cb342-4" aria-hidden="true" tabindex="-1"></a>starwars_data <span class="sc">%>%</span> </span>
<span id="cb342-5"><a href="solutions.html#cb342-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(name, species, mass, gr_mass)</span></code></pre></div>
<pre><code>## # A tibble: 87 × 4
## name species mass gr_mass
## <chr> <chr> <dbl> <dbl>
## 1 Luke Skywalker Human 77 77000
## 2 C-3PO Droid 75 75000
## 3 R2-D2 Droid 32 32000
## 4 Darth Vader Human 136 136000
## 5 Leia Organa Human 49 49000
## 6 Owen Lars Human 120 120000
## 7 Beru Whitesun lars Human 75 75000
## 8 R5-D4 Droid 32 32000
## 9 Biggs Darklighter Human 84 84000
## 10 Obi-Wan Kenobi Human 77 77000
## # … with 77 more rows</code></pre>
</div>
</div>
<div id="solutions-for-exercise-3" class="section level2 unnumbered hasAnchor">
<h2>Solutions for Exercise 3<a href="solutions.html#solutions-for-exercise-3" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<div id="task-1-7" class="section level3 unnumbered hasAnchor">
<h3>Task 1<a href="solutions.html#task-1-7" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Try to reproduce this plot with <code>dplyr</code> and <code>ggplot2</code>. (<strong>Hint:</strong> You
can hide the legend by adding <code>theme(legend.position = "none")</code> to your
plot.)</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb344"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb344-1"><a href="solutions.html#cb344-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb344-2"><a href="solutions.html#cb344-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">sex =</span> <span class="fu">case_when</span>(</span>
<span id="cb344-3"><a href="solutions.html#cb344-3" aria-hidden="true" tabindex="-1"></a> sex <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> <span class="st">"Female"</span>,</span>
<span id="cb344-4"><a href="solutions.html#cb344-4" aria-hidden="true" tabindex="-1"></a> sex <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> <span class="st">"Male"</span>)) <span class="sc">%>%</span></span>
<span id="cb344-5"><a href="solutions.html#cb344-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Party =</span> <span class="fu">case_when</span>(</span>
<span id="cb344-6"><a href="solutions.html#cb344-6" aria-hidden="true" tabindex="-1"></a> partyid <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> <span class="st">"Democrat"</span>,</span>
<span id="cb344-7"><a href="solutions.html#cb344-7" aria-hidden="true" tabindex="-1"></a> partyid <span class="sc">==</span> <span class="dv">2</span> <span class="sc">~</span> <span class="st">"Independent"</span>,</span>
<span id="cb344-8"><a href="solutions.html#cb344-8" aria-hidden="true" tabindex="-1"></a> partyid <span class="sc">==</span> <span class="dv">3</span> <span class="sc">~</span> <span class="st">"Republican"</span>)) <span class="sc">%>%</span></span>
<span id="cb344-9"><a href="solutions.html#cb344-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>Party,<span class="at">y=</span>negemot, <span class="at">fill=</span>Party)) <span class="sc">+</span></span>
<span id="cb344-10"><a href="solutions.html#cb344-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">stat_summary</span>(<span class="at">geom =</span> <span class="st">"bar"</span>, <span class="at">fun =</span> <span class="st">"mean"</span>) <span class="sc">+</span></span>
<span id="cb344-11"><a href="solutions.html#cb344-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb344-12"><a href="solutions.html#cb344-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"none"</span>) <span class="sc">+</span></span>
<span id="cb344-13"><a href="solutions.html#cb344-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Climate change attitudes of U.S. partisans by gender"</span>,</span>
<span id="cb344-14"><a href="solutions.html#cb344-14" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Negative emotions about climate change"</span>) <span class="sc">+</span></span>
<span id="cb344-15"><a href="solutions.html#cb344-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>sex, <span class="at">nrow=</span><span class="dv">2</span>)</span></code></pre></div>
</div>
<div id="task-2-7" class="section level3 unnumbered hasAnchor">
<h3>Task 2<a href="solutions.html#task-2-7" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Now, try to reproduce this graph. (<strong>Hint:</strong> You will need to recode the
<em>ideology</em> variable in a way that higher values represent stronger
attitudes, <em>independent of partisanship</em>.)</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb345"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb345-1"><a href="solutions.html#cb345-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb345-2"><a href="solutions.html#cb345-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">ideology_ext =</span> <span class="fu">case_when</span>(</span>
<span id="cb345-3"><a href="solutions.html#cb345-3" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> <span class="dv">4</span>,</span>
<span id="cb345-4"><a href="solutions.html#cb345-4" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">2</span> <span class="sc">~</span> <span class="dv">3</span>,</span>
<span id="cb345-5"><a href="solutions.html#cb345-5" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">3</span> <span class="sc">~</span> <span class="dv">2</span>,</span>
<span id="cb345-6"><a href="solutions.html#cb345-6" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">4</span> <span class="sc">~</span> <span class="dv">1</span>,</span>
<span id="cb345-7"><a href="solutions.html#cb345-7" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">5</span> <span class="sc">~</span> <span class="dv">2</span>,</span>
<span id="cb345-8"><a href="solutions.html#cb345-8" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">6</span> <span class="sc">~</span> <span class="dv">3</span>,</span>
<span id="cb345-9"><a href="solutions.html#cb345-9" aria-hidden="true" tabindex="-1"></a> ideology <span class="sc">==</span> <span class="dv">7</span> <span class="sc">~</span> <span class="dv">4</span>)) <span class="sc">%>%</span></span>
<span id="cb345-10"><a href="solutions.html#cb345-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">sex =</span> <span class="fu">case_when</span>(</span>
<span id="cb345-11"><a href="solutions.html#cb345-11" aria-hidden="true" tabindex="-1"></a> sex <span class="sc">==</span> <span class="dv">0</span> <span class="sc">~</span> <span class="st">"Female"</span>,</span>
<span id="cb345-12"><a href="solutions.html#cb345-12" aria-hidden="true" tabindex="-1"></a> sex <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> <span class="st">"Male"</span>)) <span class="sc">%>%</span></span>
<span id="cb345-13"><a href="solutions.html#cb345-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Party =</span> <span class="fu">case_when</span>(</span>
<span id="cb345-14"><a href="solutions.html#cb345-14" aria-hidden="true" tabindex="-1"></a> partyid <span class="sc">==</span> <span class="dv">1</span> <span class="sc">~</span> <span class="st">"Democrat"</span>,</span>
<span id="cb345-15"><a href="solutions.html#cb345-15" aria-hidden="true" tabindex="-1"></a> partyid <span class="sc">==</span> <span class="dv">2</span> <span class="sc">~</span> <span class="st">"Independent"</span>,</span>
<span id="cb345-16"><a href="solutions.html#cb345-16" aria-hidden="true" tabindex="-1"></a> partyid <span class="sc">==</span> <span class="dv">3</span> <span class="sc">~</span> <span class="st">"Republican"</span>))</span></code></pre></div>
<div class="sourceCode" id="cb346"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb346-1"><a href="solutions.html#cb346-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb346-2"><a href="solutions.html#cb346-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>Party,<span class="at">y=</span>ideology_ext, <span class="at">fill=</span>Party)) <span class="sc">+</span></span>
<span id="cb346-3"><a href="solutions.html#cb346-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>() <span class="sc">+</span></span>
<span id="cb346-4"><a href="solutions.html#cb346-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb346-5"><a href="solutions.html#cb346-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"none"</span>) <span class="sc">+</span></span>
<span id="cb346-6"><a href="solutions.html#cb346-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Ideological extremity of U.S. partisans by gender"</span>,</span>
<span id="cb346-7"><a href="solutions.html#cb346-7" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Ideological extremity"</span>) <span class="sc">+</span></span>
<span id="cb346-8"><a href="solutions.html#cb346-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>sex)</span></code></pre></div>
</div>
<div id="task-3-7" class="section level3 unnumbered hasAnchor">
<h3>Task 3<a href="solutions.html#task-3-7" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Can you make a chart that breaks down the relationship between <em>age</em>,
<em>negative emotions about climate change</em>, and <em>ideological extremity</em>
for the different <em>sexes</em> AND <em>parties</em>?</p>
<p><em>Solution 1:</em></p>
<div class="sourceCode" id="cb347"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb347-1"><a href="solutions.html#cb347-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span></span>
<span id="cb347-2"><a href="solutions.html#cb347-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>age,<span class="at">y=</span>negemot, <span class="at">size=</span>ideology_ext, <span class="at">color =</span> Party)) <span class="sc">+</span></span>
<span id="cb347-3"><a href="solutions.html#cb347-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb347-4"><a href="solutions.html#cb347-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="fl">0.3</span>, <span class="dv">3</span>), <span class="at">name =</span> <span class="st">"Ideological extremity"</span>) <span class="sc">+</span> <span class="co"># You can't guess the exact value that I've used here. Just use whatever looks good for you and comes close to the solution.</span></span>
<span id="cb347-5"><a href="solutions.html#cb347-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb347-6"><a href="solutions.html#cb347-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Relationship between age, climate change attitudes, </span><span class="sc">\n</span><span class="st"> and ideological extremity"</span>,</span>
<span id="cb347-7"><a href="solutions.html#cb347-7" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Age"</span>, <span class="at">y =</span> <span class="st">"Negative emotions about climate change"</span>) <span class="sc">+</span></span>
<span id="cb347-8"><a href="solutions.html#cb347-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>sex, <span class="at">nrow=</span><span class="dv">2</span>)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-199-1.png" width="672" /></p>
<p><em>Solution 2:</em></p>
<p>Alternatively, you might enjoy this look that you can create with
<code>facet_grid()</code>:</p>
<div class="sourceCode" id="cb348"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb348-1"><a href="solutions.html#cb348-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb348-2"><a href="solutions.html#cb348-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>age,<span class="at">y=</span>negemot, <span class="at">size=</span>ideology_ext, <span class="at">color =</span> Party)) <span class="sc">+</span></span>
<span id="cb348-3"><a href="solutions.html#cb348-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb348-4"><a href="solutions.html#cb348-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="fl">0.3</span>, <span class="dv">3</span>), <span class="at">name =</span> <span class="st">"Ideological extremity"</span>) <span class="sc">+</span></span>
<span id="cb348-5"><a href="solutions.html#cb348-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb348-6"><a href="solutions.html#cb348-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Relationship between age, climate change attitudes, and ideological extremity"</span>,</span>
<span id="cb348-7"><a href="solutions.html#cb348-7" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Age"</span>, <span class="at">y =</span> <span class="st">"Negative emotions about climate change"</span>) <span class="sc">+</span></span>
<span id="cb348-8"><a href="solutions.html#cb348-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="fu">vars</span>(sex), <span class="fu">vars</span>(Party))</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-200-1.png" width="672" /></p>
<p><em>Solution 3:</em></p>
<p>Or even this look, also done with <code>facet_grid()</code>:</p>
<div class="sourceCode" id="cb349"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb349-1"><a href="solutions.html#cb349-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb349-2"><a href="solutions.html#cb349-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>age,<span class="at">y=</span>negemot, <span class="at">size=</span>ideology_ext, <span class="at">color =</span> Party)) <span class="sc">+</span></span>
<span id="cb349-3"><a href="solutions.html#cb349-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb349-4"><a href="solutions.html#cb349-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_size</span>(<span class="at">range =</span> <span class="fu">c</span>(<span class="fl">0.3</span>, <span class="dv">3</span>), <span class="at">name =</span> <span class="st">"Ideological extremity"</span>) <span class="sc">+</span></span>
<span id="cb349-5"><a href="solutions.html#cb349-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb349-6"><a href="solutions.html#cb349-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Relationship between age, climate change attitudes, and ideological extremity"</span>,</span>
<span id="cb349-7"><a href="solutions.html#cb349-7" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="st">"Age"</span>, <span class="at">y =</span> <span class="st">"Negative emotions about climate change"</span>) <span class="sc">+</span></span>
<span id="cb349-8"><a href="solutions.html#cb349-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(<span class="sc">~</span>sex <span class="sc">+</span> Party)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-201-1.png" width="672" /></p>
</div>
</div>
<div id="solutions-for-exercise-4" class="section level2 unnumbered hasAnchor">
<h2>Solutions for Exercise 4<a href="solutions.html#solutions-for-exercise-4" class="anchor-section" aria-label="Anchor link to header"></a></h2>
<p>This exercise was created because some students asked to get some more
practice. You don’t have to work through it, but it can certainly help
you with the graded assignment. In this exercise, we will work with the
<code>mtcars</code> data that comes pre-installed with <code>dplyr</code>.</p>
<div class="sourceCode" id="cb350"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb350-1"><a href="solutions.html#cb350-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb350-2"><a href="solutions.html#cb350-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb350-3"><a href="solutions.html#cb350-3" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> mtcars</span>
<span id="cb350-4"><a href="solutions.html#cb350-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb350-5"><a href="solutions.html#cb350-5" aria-hidden="true" tabindex="-1"></a><span class="co"># To make the data somewhat more interesting, let's set a few values to missing values:</span></span>
<span id="cb350-6"><a href="solutions.html#cb350-6" aria-hidden="true" tabindex="-1"></a>data<span class="sc">$</span>wt <span class="ot"><-</span> <span class="fu">na_if</span>(data<span class="sc">$</span>wt, <span class="fl">4.070</span>)</span>
<span id="cb350-7"><a href="solutions.html#cb350-7" aria-hidden="true" tabindex="-1"></a>data<span class="sc">$</span>mpg <span class="ot"><-</span> <span class="fu">na_if</span>(data<span class="sc">$</span>mpg, <span class="fl">22.8</span>)</span></code></pre></div>
<div id="task-1-8" class="section level3 unnumbered hasAnchor">
<h3>Task 1<a href="solutions.html#task-1-8" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Check the data set for missing values (<em>NA</em>s) and delete all
observations that have missing values.</p>
<p><em>Solution:</em></p>
<p>You can solve this by excluding NAs in every single column:</p>
<div class="sourceCode" id="cb351"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb351-1"><a href="solutions.html#cb351-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span></span>
<span id="cb351-2"><a href="solutions.html#cb351-2" aria-hidden="true" tabindex="-1"></a> <span class="co"># we'll now only keep observations that are NOT NAs in the following variables (remember that & = AND):</span></span>
<span id="cb351-3"><a href="solutions.html#cb351-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(<span class="sc">!</span><span class="fu">is.na</span>(mpg) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(cyl) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(disp) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(hp) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(drat) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(wt) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(qsec)</span>
<span id="cb351-4"><a href="solutions.html#cb351-4" aria-hidden="true" tabindex="-1"></a> <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(vs) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(am) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(gear) <span class="sc">&</span> <span class="sc">!</span><span class="fu">is.na</span>(carb))</span></code></pre></div>
<p>Alternatively, excluding NAs from the entire data set works, too, but
you have not learned the <code>na_omit()</code>function in the tutorials:</p>
<div class="sourceCode" id="cb352"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb352-1"><a href="solutions.html#cb352-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span></span>
<span id="cb352-2"><a href="solutions.html#cb352-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">na.omit</span>()</span></code></pre></div>
</div>
<div id="task-2-8" class="section level3 unnumbered hasAnchor">
<h3>Task 2<a href="solutions.html#task-2-8" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Let’s transform the weight <em>wt</em> of the cars. Currently, it’s given as
<em>Weight in 1000 lbs</em>. I guess you are not used to lbs, so try to mutate
<em>wt</em> to represent <em>Weight in 1000 kg</em>. 1000 lbs = 453.59 kg, so we will
need to divide by 2.20.</p>
<p>Similarly, I think that you are not very familiar with the unit <em>Miles
per gallon</em> of the <em>mpg</em> variable. Let’s transform it into <em>Kilometer
per liter</em>. 1 m/g = 0.425144 km/l, so again divide by 2.20.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb353"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb353-1"><a href="solutions.html#cb353-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span></span>
<span id="cb353-2"><a href="solutions.html#cb353-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">wt =</span> wt<span class="sc">/</span><span class="fl">2.20</span>)</span></code></pre></div>
<div class="sourceCode" id="cb354"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb354-1"><a href="solutions.html#cb354-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span></span>
<span id="cb354-2"><a href="solutions.html#cb354-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">mpg =</span> mpg<span class="sc">/</span><span class="fl">2.20</span>)</span></code></pre></div>
</div>
<div id="task-3-8" class="section level3 unnumbered hasAnchor">
<h3>Task 3<a href="solutions.html#task-3-8" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Now we want to group the weight of the cars in three categories: light,
medium, heavy. But how to define light, medium, and heavy cars, i.e., at
what kg should you put the threshold? A reasonable approach is to use
quantiles (see Tutorial: <a href="tutorial-data-management-with-tidyverse.html#summarize-group_by">summarize() [+
group_by()]</a>). Quantiles divide data. For example,
the 75% quantile states that exactly 75% of the data values are equal or
below the quantile value. The rest of the values are equal or above it.</p>
<p>Use the lower quantile (0.25) and the upper quantile (0.75) to estimate
two values that divide the weight of the cars in three groups. What are
these values?</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb355"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb355-1"><a href="solutions.html#cb355-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb355-2"><a href="solutions.html#cb355-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">UQ_wt=</span> <span class="fu">quantile</span>(wt, <span class="fl">0.75</span>),</span>
<span id="cb355-3"><a href="solutions.html#cb355-3" aria-hidden="true" tabindex="-1"></a> <span class="at">LQ_wt=</span> <span class="fu">quantile</span>(wt, <span class="fl">0.25</span>))</span></code></pre></div>
<pre><code>## UQ_wt LQ_wt
## 1 1.622727 1.190909</code></pre>
<p>75% of all cars weigh 1.622727* 1000kg or less and 25% of all cars
weigh 1.190909* 1000kg or less.</p>
</div>
<div id="task-4-6" class="section level3 unnumbered hasAnchor">
<h3>Task 4<a href="solutions.html#task-4-6" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Use the values from Task 3 to create a new variable <em>wt_cat</em> that
divides the cars in three groups: light, medium, and heavy cars.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb357"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb357-1"><a href="solutions.html#cb357-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb357-2"><a href="solutions.html#cb357-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">wt_cat =</span> <span class="fu">case_when</span>(</span>
<span id="cb357-3"><a href="solutions.html#cb357-3" aria-hidden="true" tabindex="-1"></a> wt <span class="sc"><=</span> <span class="fl">1.190909</span> <span class="sc">~</span> <span class="st">"light car"</span>,</span>
<span id="cb357-4"><a href="solutions.html#cb357-4" aria-hidden="true" tabindex="-1"></a> wt <span class="sc"><</span> <span class="fl">1.622727</span> <span class="sc">~</span> <span class="st">"medium car"</span>,</span>
<span id="cb357-5"><a href="solutions.html#cb357-5" aria-hidden="true" tabindex="-1"></a> wt <span class="sc">>=</span> <span class="fl">1.622727</span> <span class="sc">~</span> <span class="st">"heavy car"</span>))</span></code></pre></div>
</div>
<div id="task-5-6" class="section level3 unnumbered hasAnchor">
<h3>Task 5<a href="solutions.html#task-5-6" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>How many light, medium, and heavy cars are part of the data?</p>
<p><em>Solution:</em></p>
<p>You can solve this with the <code>summarize(count = n()</code> function:</p>
<div class="sourceCode" id="cb358"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb358-1"><a href="solutions.html#cb358-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb358-2"><a href="solutions.html#cb358-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(wt_cat) <span class="sc">%>%</span> </span>
<span id="cb358-3"><a href="solutions.html#cb358-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">count =</span> <span class="fu">n</span>())</span></code></pre></div>
<pre><code>## # A tibble: 3 × 2
## wt_cat count
## <chr> <int>
## 1 heavy car 9
## 2 light car 7
## 3 medium car 13</code></pre>
<p>9 heavy cars, 13 medium cars, and 7 light cars.</p>
<p>Alternatively, you can also use the <code>count()</code> function:</p>
<div class="sourceCode" id="cb360"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb360-1"><a href="solutions.html#cb360-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb360-2"><a href="solutions.html#cb360-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">count</span>(wt_cat)</span></code></pre></div>
<pre><code>## wt_cat n
## 1 heavy car 9
## 2 light car 7
## 3 medium car 13</code></pre>
</div>
<div id="task-6-6" class="section level3 unnumbered hasAnchor">
<h3>Task 6<a href="solutions.html#task-6-6" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Now sort this count of the car weight classes from highest to lowest.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb362"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb362-1"><a href="solutions.html#cb362-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb362-2"><a href="solutions.html#cb362-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">group_by</span>(wt_cat) <span class="sc">%>%</span> </span>
<span id="cb362-3"><a href="solutions.html#cb362-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">summarize</span>(<span class="at">count =</span> <span class="fu">n</span>()) <span class="sc">%>%</span> </span>
<span id="cb362-4"><a href="solutions.html#cb362-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">arrange</span>(<span class="fu">desc</span>(count))</span></code></pre></div>
<pre><code>## # A tibble: 3 × 2
## wt_cat count
## <chr> <int>
## 1 medium car 13
## 2 heavy car 9
## 3 light car 7</code></pre>
</div>
<div id="task-7-4" class="section level3 unnumbered hasAnchor">
<h3>Task 7<a href="solutions.html#task-7-4" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Make a scatter plot to indicate how many km per liter (<em>mpg</em>) a car can
drive depending on its weight (<em>wt</em>). Facet the plot by weight class
(<em>wt_cat</em>). Try to hide the plot legend (you have learned that in
another exercise).</p>
<div class="sourceCode" id="cb364"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb364-1"><a href="solutions.html#cb364-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span> </span>
<span id="cb364-2"><a href="solutions.html#cb364-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">wt_cat =</span> <span class="fu">factor</span>(wt_cat, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"light car"</span>, <span class="st">"medium car"</span>, <span class="st">"heavy car"</span>))) <span class="sc">%>%</span> </span>
<span id="cb364-3"><a href="solutions.html#cb364-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>wt, <span class="at">y=</span>mpg, <span class="at">color=</span>wt_cat)) <span class="sc">+</span></span>
<span id="cb364-4"><a href="solutions.html#cb364-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb364-5"><a href="solutions.html#cb364-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb364-6"><a href="solutions.html#cb364-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#7b3294"</span>, <span class="st">"#84798a"</span>, <span class="st">"#008837"</span>)) <span class="sc">+</span> <span class="co"># optional command, choose your own beautiful colors for the graph</span></span>
<span id="cb364-7"><a href="solutions.html#cb364-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"none"</span>) <span class="sc">+</span></span>
<span id="cb364-8"><a href="solutions.html#cb364-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Relationship between car weight and achieved kilometers per liter"</span>, <span class="at">x=</span><span class="st">"Weight in 1000kg"</span>, <span class="at">y=</span><span class="st">"km/l"</span>) <span class="sc">+</span></span>
<span id="cb364-9"><a href="solutions.html#cb364-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>wt_cat)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-212-1.png" width="672" /></p>
</div>
<div id="task-8-1" class="section level3 unnumbered hasAnchor">
<h3>Task 8<a href="solutions.html#task-8-1" class="anchor-section" aria-label="Anchor link to header"></a></h3>
<p>Recreate the diagram from Task 7, but exclude all cars that weigh
between 1.4613636 and 1.5636364 *1000kg from it.</p>
<p><em>Solution:</em></p>
<div class="sourceCode" id="cb365"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb365-1"><a href="solutions.html#cb365-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span></span>
<span id="cb365-2"><a href="solutions.html#cb365-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(wt <span class="sc"><</span> <span class="fl">1.4613636</span> <span class="sc">|</span> wt <span class="sc">></span> <span class="fl">1.5636364</span>) <span class="sc">%>%</span> </span>
<span id="cb365-3"><a href="solutions.html#cb365-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">wt_cat =</span> <span class="fu">factor</span>(wt_cat, <span class="at">levels =</span> <span class="fu">c</span>(<span class="st">"light car"</span>, <span class="st">"medium car"</span>, <span class="st">"heavy car"</span>))) <span class="sc">%>%</span> </span>
<span id="cb365-4"><a href="solutions.html#cb365-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(<span class="at">x=</span>wt, <span class="at">y=</span>mpg, <span class="at">color=</span>wt_cat)) <span class="sc">+</span></span>
<span id="cb365-5"><a href="solutions.html#cb365-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>() <span class="sc">+</span></span>
<span id="cb365-6"><a href="solutions.html#cb365-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme_bw</span>() <span class="sc">+</span></span>
<span id="cb365-7"><a href="solutions.html#cb365-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">scale_color_manual</span>(<span class="at">values =</span> <span class="fu">c</span>(<span class="st">"#7b3294"</span>, <span class="st">"#84798a"</span>, <span class="st">"#008837"</span>)) <span class="sc">+</span> <span class="co"># optional command, choose your own beautiful colors for the graph</span></span>
<span id="cb365-8"><a href="solutions.html#cb365-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">theme</span>(<span class="at">legend.position =</span> <span class="st">"none"</span>) <span class="sc">+</span></span>
<span id="cb365-9"><a href="solutions.html#cb365-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">title =</span> <span class="st">"Relationship between car weight and achieved kilometers per liter"</span>, <span class="at">x=</span><span class="st">"Weight in 1000kg"</span>, <span class="at">y=</span><span class="st">"km/l"</span>) <span class="sc">+</span></span>
<span id="cb365-10"><a href="solutions.html#cb365-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span>wt_cat)</span></code></pre></div>
<p><img src="PR-Erfolgskontrolle_files/figure-html/unnamed-chunk-213-1.png" width="672" /></p>
<p><strong>Why would we use <code>data %>% filter(wt < 1.4613636 | wt > 1.5636364)</code>
instead of <code>data %>% filter(wt > 1.4613636 | wt < 1.5636364)</code>?</strong></p>
<p>Let’s look at the resulting data sets when you apply those filters to
compare them:</p>
<div class="sourceCode" id="cb366"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb366-1"><a href="solutions.html#cb366-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span></span>
<span id="cb366-2"><a href="solutions.html#cb366-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(wt) <span class="sc">%>%</span> </span>
<span id="cb366-3"><a href="solutions.html#cb366-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(wt <span class="sc"><</span> <span class="fl">1.4613636</span> <span class="sc">|</span> wt <span class="sc">></span> <span class="fl">1.5636364</span>)</span></code></pre></div>
<pre><code>## wt
## Mazda RX4 1.1909091
## Mazda RX4 Wag 1.3068182
## Valiant 1.5727273
## Duster 360 1.6227273
## Merc 240D 1.4500000
## Merc 450SL 1.6954545
## Merc 450SLC 1.7181818
## Cadillac Fleetwood 2.3863636
## Lincoln Continental 2.4654545
## Chrysler Imperial 2.4295455
## Fiat 128 1.0000000
## Honda Civic 0.7340909
## Toyota Corolla 0.8340909
## Toyota Corona 1.1204545
## Dodge Challenger 1.6000000
## Camaro Z28 1.7454545
## Pontiac Firebird 1.7477273
## Fiat X1-9 0.8795455
## Porsche 914-2 0.9727273
## Lotus Europa 0.6877273
## Ford Pantera L 1.4409091
## Ferrari Dino 1.2590909
## Maserati Bora 1.6227273
## Volvo 142E 1.2636364</code></pre>
<p>The resulting table does not include any cars that weigh between
1.4613636 and 1.5636364. But if you use
<code>data %>% filter(wt > 1.4613636 | wt < 1.5636364)</code>…</p>
<div class="sourceCode" id="cb368"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb368-1"><a href="solutions.html#cb368-1" aria-hidden="true" tabindex="-1"></a>data <span class="sc">%>%</span></span>
<span id="cb368-2"><a href="solutions.html#cb368-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(wt) <span class="sc">%>%</span> </span>
<span id="cb368-3"><a href="solutions.html#cb368-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(wt <span class="sc">></span> <span class="fl">1.4613636</span> <span class="sc">|</span> wt <span class="sc"><</span> <span class="fl">1.5636364</span>)</span></code></pre></div>
<pre><code>## wt
## Mazda RX4 1.1909091
## Mazda RX4 Wag 1.3068182
## Hornet 4 Drive 1.4613636
## Hornet Sportabout 1.5636364
## Valiant 1.5727273
## Duster 360 1.6227273
## Merc 240D 1.4500000
## Merc 280 1.5636364
## Merc 280C 1.5636364
## Merc 450SL 1.6954545
## Merc 450SLC 1.7181818
## Cadillac Fleetwood 2.3863636
## Lincoln Continental 2.4654545
## Chrysler Imperial 2.4295455
## Fiat 128 1.0000000
## Honda Civic 0.7340909
## Toyota Corolla 0.8340909
## Toyota Corona 1.1204545
## Dodge Challenger 1.6000000