-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
2723 lines (2150 loc) · 119 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<title>METRO, Intro to R Workshop</title>
<meta charset="utf-8" />
<meta name="author" content="Dan Simonet" />
<meta name="date" content="2021-11-15" />
<script src="libs/header-attrs/header-attrs.js"></script>
<link href="libs/remark-css/default.css" rel="stylesheet" />
<link href="libs/remark-css/MSU.css" rel="stylesheet" />
<link href="libs/remark-css/rutgers-fonts.css" rel="stylesheet" />
<link href="libs/remark-css/ninjutsu.css" rel="stylesheet" />
<link href="libs/panelset/panelset.css" rel="stylesheet" />
<script src="libs/panelset/panelset.js"></script>
<script src="libs/kePrint/kePrint.js"></script>
<link href="libs/lightable/lightable.css" rel="stylesheet" />
</head>
<body>
<textarea id="source">
class: center, middle, inverse, title-slide
# METRO, Intro to R Workshop
### Dan Simonet
### Associate Professor, I/O Psychology
### 2021-11-15
---
class: inverse, center, middle
# Who Am I
---
# R Journey
.panelset[
.panel[.panel-name[Beginning]
<div style="text-align: center;">
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/SPSS.png?raw=true" width = "300" height = "200" style="margin: 60px">
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/Excel.jpg?raw=true" width = "300" height = "200" style="margin: 60px">
</div>
]
.panel[.panel-name[Robust Stats]
<div style="text-align: center;">
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/RobustStats.jpg?raw=true" width="300" height="400" style="margin: 40px">
</div>
]
.panel[.panel-name[Polynomial Paper]
<div style="text-align: center;">
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/RSAforPersonality.png?raw=true" width="400" height="375" style="margin: 40px">
</div>
.footnote[Source: Koppensteiner et al. (2014)]
]
.panel[.panel-name[New Possibilities]
<div style="text-align: center; vertical-align: text-top">
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/Network.png?raw=true" width="700" height="445" style="margin: 0px 0px 30px 0px">
</div>
]
]
---
class: inverse, center, middle
# R Applications
---
class: inverse, center, middle
background-image: url("https://github.com/DanSimonet/METRO-Workshop/blob/main/img/2020_29_Astronauts_Cedric.png?raw=true")
background-size: contain
# Visuals
.footnote[.tiny[.green[Image Credit: ][Cedric Scherer](https://www.behance.net/gallery/100683383/Travelling-to-Outer-Space)]]
---
class: inverse, top, middle
background-image: url("https://raw.githubusercontent.com/DanSimonet/METRO-Workshop/main/img/got-network-algorithm.png")
background-position: right
background-size: contain
# Algorithms
.footnote[.tiny[.white[Image Credit: ][Andrew Beveridge](https://www.maa.org/sites/default/files/pdf/Mathhorizons/NetworkofThrones%20%281%29.pdf)]]
---
# Reports
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/Rep1.png?raw=true" width = "450" height = "500" align="left">
<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/Rep2.png?raw=true" width = "400" height = "500" align="right">
---
# Why R?
1. **Free**: As in Beer, as in Speech 🍺
2. **AMAZING** community continuously contributing new packages 🤗
3. **Comprehensive**: Clean, visualize, analyze, and communicate. Can even order pizza 🍕
4. **Level Up**: Programming superpower - generate new solutions 💪
5. **Speed Up**: Reproducibility and automation 🚀
--
.center[**Employability**]
.center[<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/gif/LikeaBoss.gif?raw=true" width="500" height="200">]
---
# Cons
- **Unfamiliarity**: Learning a new language (can) suck. Go easy on yourself and R.
- Quick Tips: Start slow with Base R books and do **lots** of practice problems. Suggestions include:
- [Hands-On Programming with R](https://rstudio-education.github.io/hopr/)
- [Learning Statistics with R](https://learningstatisticswithr.com/)
- [Quick R](https://www.statmethods.net/index.html)
--
- **Paradox of Choice**: R invariably has multiple ways to do the same thing. Example includes assigning objects with <- and/or = (convention is to use <-).
- Quick tip: Just pick whatever method/package looks easiest to you. At same time, don't be scared to mix and match.
---
# Learning Curve: Takes Work
#### Complicated Relationship
![](https://github.com/DanSimonet/DanSimonet.github.io/blob/master/Figures/rrelationships_R.png?raw=true)
.footer[source: https://bookdown.org/ndphillips/YaRrr/rrelationship.html]
---
# Learning Curve: Takes Work
#### Zero to Hero
![](https://github.com/DanSimonet/METRO-Workshop/blob/main/img/gosling.png?raw=true)
---
# Help
_Very_ friendly communities and forums online. SO, RLadies, Twitter, etc.
You can also look at the help documentation.
```r
?plot
example(plot)
library(ggplot2)
example(geom_contour)
demo("graphics", package = "graphics")
```
Most packages also provide _vignettes_ (mini use guides). [Example](https://lrberge.github.io/fixest/articles/fixest_walkthrough.html).
---
# Agenda
.pull-left[
### R and RStudio
### Data Manipulation and Descriptives
### Visualization
### Statistics and Markdown Reports
### New Frontiers: NLP and Twitter
]
.pull-right[
![agenda](https://github.com/DanSimonet/METRO-Workshop/blob/main/img/meeting-agenda.jpg?raw=true)
]
---
# Requirements
We will be using the [RStudio Cloud](https://rstudio.cloud/) which has all data and packages installed. Must create an account, sign in, and access the following project:
-https://rstudio.cloud/project/3226006
Once accessed (a) click on the Load.R script, (b) highlight all the syntax, and (c) click the Run icon.
.center[<img src="https://github.com/DanSimonet/METRO-Workshop/blob/main/img/LoadR.PNG?raw=true" width="1000" height="350">]
---
# Requirements
Alternatively, can install R and R studio locally on your machine and install the packages as we progress.
☑ Installed [R](https://www.r-project.org/).
☑ Installed [RStudio](https://www.rstudio.com/products/rstudio/download/preview/).
---
class: inverse, center, middle
# R and R Studio
---
class: top, left
background-image: url("https://github.com/DanSimonet/METRO-Workshop/blob/main/img/rstudio-anatomy.png?raw=true")
background-size: contain
---
# Base R
.pull-left[
#### Maths
```r
1+1
sqrt(4)
5^2
```
#### Objects
```r
x <- 3 + 5
x * 2
x + x
```
#### Vectorization
```r
y <- c(1,2,3,4,5) "combine" to vector
z <- seq(0,10, by = 2) **seq** func
2 * y
y/z
```
]
--
.pull-right[
####Functions
```r
mean(1:5) # base
add_two <- function(x){ # custom
x+2
}
```
####Packages
```r
library(ggplot2)
ggplot(mtcars, aes(x = hp, y = mpg)) +
geom_point() +
geom_smooth(method = "lm", se = F) +
labs(x = "Horsepower",
y = "Miles per Gallon (MPG)")
```
]
---
class: inverse, center, middle
# Data Manipulation
---
# Load and View Data
Use **`install.packages`** and **`library`** to install and load packages. **`read_csv`** function parses and imports **.csv** files.
```r
# Load Tidyverse and IBM HR Data
install.packages("tidyverse")
library(tidyverse)
HR_dat <- read_csv("https://raw.githubusercontent.com/DanSimonet/METRO-Workshop/main/Data/IBM_HR_Data.csv")
```
--
Quick data examination:
- First 5 rows with **`head`**
- Structure with **`glimpse`**
- Variable labels with **`names`**
---
# Data Science Workflow with **Tidyverse**
![](https://github.com/DanSimonet/METRO-Workshop/blob/main/img/tidyverseworkflow.png?raw=true)
---
# The pipe %>%
```r
mean(1:10)
```
```
## [1] 5.5
```
```r
1:10 %>% mean()
```
```
## [1] 5.5
```
---
# The pipe %>%
```r
data %>%
do_something(.) %>%
do_another_thing(.) %>%
do_last_thing(.)
```
**`do_last_thing(do_something_else(do_something(data)))`** is equivalent to:
- **`data %>% do_something(data = .)`**
--
- **`%>% do_something_else(.)`**
--
- **`%>% do_a_third_thing(.)`**
--
<br></br>
For example, we may run several cleaning operations and store as new dataset.
```r
new_cars <- mtcars %>%
mutate(cyl = factor(cyl)) %>% # convert to factor
select(mpg, cyl, hp) # reduce to a few variables
```
---
# **`Dplyr`**: Data Wrangling
.pull-left[
Data sets are messy, large, and disorganized
**`dplyr`** provides common data manipulation **verbs**
which are applied to tabular data. Includes:
- Extracting and renaming variables
- Extracting a subset of rows
- Ordering data from high to low
- Calculating new variables (from existing)
- Calculating summary statistics
- Merging different data frames
- And more!
]
.pull-right[
![dplyr](https://github.com/DanSimonet/METRO-Workshop/blob/main/img/dplyr_schema.png?raw=true)
]
---
# Select and Drop Columns
.pull-left[
```r
HR_dat %>% select(Age, JobLevel, Attrition)
```
```
## # A tibble: 1,470 x 3
## Age JobLevel Attrition
## <dbl> <dbl> <chr>
## 1 41 2 Yes
## 2 49 2 No
## 3 37 1 Yes
## 4 33 1 No
## 5 27 1 No
## # ... with 1,465 more rows
```
```r
HR_dat %>% select(Age:BusinessTravel)
```
```
## # A tibble: 1,470 x 3
## Age Attrition BusinessTravel
## <dbl> <chr> <chr>
## 1 41 Yes Travel_Rarely
## 2 49 No Travel_Frequently
## 3 37 Yes Travel_Rarely
## 4 33 No Travel_Frequently
## 5 27 No Travel_Rarely
## # ... with 1,465 more rows
```
]
--
.pull-right[
```r
HR_dat %>% select(-Attrition)
```
```
## # A tibble: 1,470 x 34
## Age BusinessTravel DailyRate Department
## <dbl> <chr> <dbl> <chr>
## 1 41 Travel_Rarely 1102 Sales
## 2 49 Travel_Frequent~ 279 Research & Dev~
## 3 37 Travel_Rarely 1373 Research & Dev~
## 4 33 Travel_Frequent~ 1392 Research & Dev~
## 5 27 Travel_Rarely 591 Research & Dev~
## # ... with 1,465 more rows, and 30 more variable:
## # DistanceFromHome <dbl>, ...
```
```r
HR_dat %>% select(contains("Job"))
```
```
## # A tibble: 1,470 x 4
## JobInvolvement JobLevel JobRole
## <dbl> <dbl> <chr>
## 1 3 2 Sales Executi~
## 2 2 2 Research Scie~
## 3 2 1 Laboratory Te~
## 4 3 1 Research Scie~
## 5 3 1 Laboratory Te~
## # ... with 1,465 more rows, and 1 more
## # variable: JobSatisfaction <dbl>
```
]
---
# Useful Select Functions
### **Red functions come in dplyr**
<table class=" lightable-minimal" style="font-size: 28px; font-family: Yanone Kaffeesatz; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;"> Function </th>
<th style="text-align:left;"> Explanation </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;"> - </td>
<td style="text-align:left;"> Select everything but </td>
</tr>
<tr>
<td style="text-align:left;"> : </td>
<td style="text-align:left;"> Select range </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> contains() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select columns whose name contains a character string </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> ends_with() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select columns whose name ends with a string </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> everything() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select every column </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> matches() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select columns who name matches a regular expression </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> num_range() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select columns names x1, x2, x3, x4, x5 </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> one_of() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select columns whose names are in a group of names </td>
</tr>
<tr>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> starts_with() </td>
<td style="text-align:left;color: white !important;background-color: #D7261E !important;"> Select columns whose name starts with a character string </td>
</tr>
</tbody>
</table>
---
# Filter Cases
Select only males
```r
HR_dat %>% filter(Gender == "Male")
```
```
## # A tibble: 882 x 35
## Gender Age Attrition BusinessTravel DailyRate
## <chr> <dbl> <chr> <chr> <dbl>
## 1 Male 49 No Travel_Freque~ 279
## 2 Male 37 Yes Travel_Rarely 1373
## 3 Male 27 No Travel_Rarely 591
## 4 Male 32 No Travel_Freque~ 1005
## 5 Male 30 No Travel_Rarely 1358
## 6 Male 38 No Travel_Freque~ 216
## 7 Male 36 No Travel_Rarely 1299
## 8 Male 35 No Travel_Rarely 809
## 9 Male 31 No Travel_Rarely 670
## 10 Male 34 No Travel_Rarely 1346
## # ... with 872 more rows, and 30 more variable:
## # Department <chr>, ...
```
---
# Filter Cases
Select only employees over 40
```r
HR_dat %>% filter(Age > 40)
```
```
## # A tibble: 465 x 35
## Age Attrition BusinessTravel DailyRate
## <dbl> <chr> <chr> <dbl>
## 1 41 Yes Travel_Rarely 1102
## 2 49 No Travel_Frequently 279
## 3 59 No Travel_Rarely 1324
## 4 53 No Travel_Rarely 1219
## 5 53 No Travel_Rarely 1282
## 6 42 No Travel_Rarely 691
## 7 44 No Travel_Rarely 477
## 8 46 No Travel_Rarely 705
## 9 44 No Travel_Rarely 1459
## 10 43 No Travel_Rarely 1273
## # ... with 455 more rows, and 31 more variable:
## # Department <chr>, ...
```
---
# Filter Cases
Select only employees who are Male and 40
```r
HR_dat %>% filter(Gender == "Male", Age > 40)
```
```
## # A tibble: 272 x 35
## Gender Age Attrition BusinessTravel DailyRate
## <chr> <dbl> <chr> <chr> <dbl>
## 1 Male 49 No Travel_Freque~ 279
## 2 Male 42 No Travel_Rarely 691
## 3 Male 44 No Travel_Rarely 1459
## 4 Male 50 Yes Travel_Rarely 869
## 5 Male 46 No Travel_Freque~ 1211
## 6 Male 48 Yes Travel_Rarely 626
## 7 Male 45 No Travel_Rarely 1339
## 8 Male 45 No Travel_Rarely 193
## 9 Male 46 No Travel_Rarely 945
## 10 Male 55 No Travel_Rarely 111
## # ... with 262 more rows, and 30 more variable:
## # Department <chr>, ...
```
---
# Filter: Logical Tests in R
.pull-left[
###?Comparison
<table class=" lightable-classic" style="font-size: 30px; font-family: Yanone Kaffeesatz; width: auto !important; float: left; margin-right: 10px;">
<tbody>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> &lt; </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Less than </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> &gt; </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Greater than </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> == </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Equal to </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> &lt;= </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Less than or equal to </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> &gt;= </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Greater than or equal to </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> != </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Not equal to </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> %in% </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Group membership </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> is.na </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> Is NA </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> !is.na </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> is not NA </td>
</tr>
</tbody>
</table>
]
.pull-right[
###?base::Logic
<table class=" lightable-classic" style="font-size: 30px; font-family: Yanone Kaffeesatz; width: auto !important; float: left; margin-right: 10px;">
<tbody>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> &amp; </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> boolean and </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> | </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> boolean or </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> xor </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> exactly or </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> ! </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> not </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> any </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> any true </td>
</tr>
<tr>
<td style="text-align:left;width: 5em; background-color: #F5F5F5 !important;"> all </td>
<td style="text-align:left;background-color: #F5F5F5 !important;"> all true </td>
</tr>
</tbody>
</table>
]
---
# Mutate: Create New Variables
- Data transformation (log, square root, %)
- Composites (addition, multiplication)
- Change factor orders
- Center or lag variables
```r
HR_dat %>%
mutate(AnnualIncome = MonthlyIncome * 12,
WorkLifeAtComp = YearsAtCompany/TotalWorkingYears)
```
```
## # A tibble: 1,470 x 5
## MonthlyIncome AnnualIncome TotalWorkingYears YearsAtCompany
## <dbl> <dbl> <dbl> <dbl>
## 1 5993 71916 8 6
## 2 5130 61560 10 10
## 3 2090 25080 7 0
## 4 2909 34908 8 8
## 5 3468 41616 6 2
## # ... with 1,465 more rows, and 1 more variable: LifeAtComp <dbl>
```
---
# Mutate
Convert attrition to a factor (for analyses) and average correlated tenure variables<sup>1</sup>
<br></br>
.footnote[<sup>1</sup> To use `mean` for individual rows across columns (e.g,. scoring scales) use `rowwise` prior to mutate]
```r
HR_dat %>%
mutate(Attrition = factor(Attrition),
TenComp = (YearsAtCompany + YearsInCurrentRole + YearsWithCurrManager)/3)
```
```
## # A tibble: 1,470 x 5
## Attrition YearsAtCompany YearsInCurrentRole YearsWithCurrManager
## <fct> <dbl> <dbl> <dbl>
## 1 Yes 6 4 5
## 2 No 10 7 7
## 3 Yes 0 0 0
## 4 No 8 7 0
## 5 No 2 2 2
## # ... with 1,465 more rows, and 1 more variable: TenComp <dbl>
```
<br></br>
---
# Arrange
Rearrange orders of values based on data in columns
```r
HR_dat %>% arrange(MonthlyIncome)
```
.pull-left[
<table class=" lightable-classic" style="font-size: 20px; font-family: Yanone Kaffeesatz; width: auto !important; float: left; margin-right: 10px;">
<thead>
<tr>
<th style="text-align:right;"> Age </th>
<th style="text-align:left;"> Department </th>
<th style="text-align:right;"> DistanceFromHome </th>
<th style="text-align:right;"> MonthlyIncome </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 41 </td>
<td style="text-align:left;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> Sales </td>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 1 </td>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 5993 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 49 </td>
<td style="text-align:left;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 8 </td>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 5130 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 37 </td>
<td style="text-align:left;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 2090 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 33 </td>
<td style="text-align:left;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 3 </td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 2909 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> 27 </td>
<td style="text-align:left;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> 3468 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> 32 </td>
<td style="text-align:left;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> 3068 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> 59 </td>
<td style="text-align:left;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> 3 </td>
<td style="text-align:right;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> 2670 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> 30 </td>
<td style="text-align:left;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> 24 </td>
<td style="text-align:right;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> 2693 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> 38 </td>
<td style="text-align:left;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> 23 </td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> 9526 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> 36 </td>
<td style="text-align:left;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> 27 </td>
<td style="text-align:right;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> 5237 </td>
</tr>
</tbody>
</table>
]
.pull-right[
<table class=" lightable-classic" style="font-size: 20px; font-family: Yanone Kaffeesatz; width: auto !important; float: right; margin-left: 10px;">
<thead>
<tr>
<th style="text-align:right;"> Age </th>
<th style="text-align:left;"> Department </th>
<th style="text-align:right;"> DistanceFromHome </th>
<th style="text-align:right;"> MonthlyIncome </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 37 </td>
<td style="text-align:left;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 2090 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> 59 </td>
<td style="text-align:left;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> 3 </td>
<td style="text-align:right;color: white !important;background-color: rgba(72, 40, 120, 1) !important;"> 2670 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> 30 </td>
<td style="text-align:left;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> 24 </td>
<td style="text-align:right;color: white !important;background-color: rgba(62, 74, 137, 1) !important;"> 2693 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 33 </td>
<td style="text-align:left;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 3 </td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 2909 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> 32 </td>
<td style="text-align:left;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(38, 130, 142, 1) !important;"> 3068 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> 27 </td>
<td style="text-align:left;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(31, 158, 137, 1) !important;"> 3468 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 49 </td>
<td style="text-align:left;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 8 </td>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 5130 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> 36 </td>
<td style="text-align:left;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> 27 </td>
<td style="text-align:right;color: white !important;background-color: rgba(108, 205, 90, 1) !important;"> 5237 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 41 </td>
<td style="text-align:left;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> Sales </td>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 1 </td>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 5993 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> 38 </td>
<td style="text-align:left;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> 23 </td>
<td style="text-align:right;color: white !important;background-color: rgba(253, 231, 37, 1) !important;"> 9526 </td>
</tr>
</tbody>
</table>
]
---
# Arrange
Can change from highest to lowest with `desc`
```r
HR_dat %>% arrange(desc(MonthlyIncome))
```
.left-pull[
<table class=" lightable-classic" style="font-size: 20px; font-family: Yanone Kaffeesatz; width: auto !important; float: left; margin-right: 10px;">
<thead>
<tr>
<th style="text-align:right;"> Age </th>
<th style="text-align:left;"> Department </th>
<th style="text-align:right;"> DistanceFromHome </th>
<th style="text-align:right;"> MonthlyIncome </th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 41 </td>
<td style="text-align:left;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> Sales </td>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 1 </td>
<td style="text-align:right;color: white !important;background-color: rgba(181, 222, 43, 1) !important;"> 5993 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 49 </td>
<td style="text-align:left;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 8 </td>
<td style="text-align:right;color: white !important;background-color: rgba(53, 183, 121, 1) !important;"> 5130 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 37 </td>
<td style="text-align:left;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 2 </td>
<td style="text-align:right;color: white !important;background-color: rgba(68, 1, 84, 1) !important;"> 2090 </td>
</tr>
<tr>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 33 </td>
<td style="text-align:left;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> Research &amp; Development </td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 3 </td>
<td style="text-align:right;color: white !important;background-color: rgba(49, 104, 142, 1) !important;"> 2909 </td>
</tr>
<tr>