-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro_to_r.html
893 lines (823 loc) · 45.9 KB
/
intro_to_r.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
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Introduction to R</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/default.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
</head>
<body>
<div class="container-fluid main-container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">IS709 - Introduction to Data Science</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">Home</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Tutorials
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="installation.html">R/RStudio Installation</a>
</li>
<li>
<a href="azure_notebooks.html">Azure Notebooks</a>
</li>
<li>
<a href="intro_to_r.html">Introduction to R</a>
</li>
<li>
<a href="amelia.html">Missing Data Imputation</a>
</li>
<li>
<a href="discretization.html">Dimensionality Reduction and Discretization</a>
</li>
<li>
<a href="classification.html">Classification</a>
</li>
<li>
<a href="clustering.html">Clustering</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://mehmetaliakyol.com/">
<span class="fa fa-question fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
<h1 class="title toc-ignore">Introduction to R</h1>
</div>
<p>This is an <a href="http://rmarkdown.rstudio.com">R Markdown</a> Notebook. When you execute code within the notebook, the results appear beneath the code. The purpose of this notebook is to enable you to play around with basic functionalities of R.</p>
<p>The website you see now is a rendered HTML file. If you want to play with the .Rmd file, you can go to the <a href="https://github.com/makyol/datascience">original repository</a>, download the .Rmd file and run it on RStudio.</p>
<div id="data-structures-of-r" class="section level2">
<h2>Data Structures of R</h2>
<pre class="r"><code>first_variable = 'This is my first R variable.'
print(first_variable)</code></pre>
<pre><code>## [1] "This is my first R variable."</code></pre>
<p>R is a powerful analytical language that is very simple to use. Although it isn’t without its quirks so we are going to look at its data types and simple functionalities first.</p>
<div id="scalar" class="section level3">
<h3>Scalar</h3>
<p>Scalar is a variable with single value assigned to it. In R, there are no singly-valued variables (i.e. scalars), instead scalars in R are simply 0-dimensional vectors.</p>
<p>There are various types in R such as character, numeric (float), integer etc. Let’s create example variables to demonstrate.</p>
<pre class="r"><code># An integer. Also you can use comments with #
lecture_code = 709
# A character variable
lecture_name = 'Introduction to Data Science'
# A float
random_float = 1.2</code></pre>
</div>
<div id="vector" class="section level3">
<h3>Vector</h3>
<p>Vector is an R structure that holds values of the same type (e.g. numeric, character). Vectors are 1-dimensional data structures. The only dimension they have is called “length”.</p>
<p>There are several ways to assign vectors to a variable. They are listed below and they are all identical:</p>
<pre class="r"><code># An integer vector
dept_lecture_code = c(901, 580)
# A character vector
dept_lecture_name = c("Information Systems", "Introduction to Data Science")</code></pre>
<p>You can also use <code>assign</code> function to create vectors:</p>
<pre class="r"><code>assign("lecture_prof_assistant",c("Tugba Taşkaya Temizel","Mehmet Ali Akyol"))</code></pre>
<p>You can concatenate vectors and assign to a new vector:</p>
<pre class="r"><code>codes_names_vector = c(dept_lecture_code, dept_lecture_name, lecture_prof_assistant)</code></pre>
<p>As you can see <code>c</code> function stands for ‘combine’. It can combine explicit values or different vectors together to form a new vector.</p>
<div id="generating-sequences-and-assigning-to-vectors" class="section level4">
<h4>Generating Sequences and Assigning to Vectors</h4>
<p>Sequences are numerical values that follow a certain rule such as numbers from 1 to 30. In R, there are two ways to generate sequences: (1) using <code>:</code> operator and (2) <code>seq</code> command.</p>
<p><code>:</code> operator generates a sequence of numbers with 1 difference between them. For instance, 1:30 is the vector c(1,2,…,30). The colon operator has priority within expression. For example, 2*1:15 is the vector c(2,4,…,28,30). 30:1 generates sequence backwards.</p>
<pre class="r"><code>#generating sequence with : operator
one_to_thirty=1:30
print(one_to_thirty)</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30</code></pre>
<pre class="r"><code>#the priority of : operator
even_numbers_to_thirty=2*1:15
print(even_numbers_to_thirty)</code></pre>
<pre><code>## [1] 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30</code></pre>
<pre class="r"><code>#backward sequence
backward_sequence=30:1
print(backward_sequence)</code></pre>
<pre><code>## [1] 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8
## [24] 7 6 5 4 3 2 1</code></pre>
<p><code>seq</code> command is a more general way to create sequences. It has 5 arguments, only some of which may be specified in any one call. The first two arguments, if given, specify the beginning and end of the sequence. If these are the only two arguments given, the result is the same as the colon operator.</p>
<pre class="r"><code>#generating sequence with seq command
seq(1,30)</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30</code></pre>
<p>The first two arguments can be named <code>from=value</code> and <code>to=value</code>. See the examples below:</p>
<pre class="r"><code>#generating sequence with named arguments in seq command
seq(from=1,to=30)</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30</code></pre>
<pre class="r"><code>seq(to=30,from=1)</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30</code></pre>
<p>The next two arguments to <code>seq()</code> command may be named as <code>by=value</code> and <code>length=value</code> which specify the step size and the length for the sequence respectively. If neither of these arguments is given, the default <code>by=1</code> is assumed.</p>
<pre class="r"><code>#by and length arguments of seq command
seq(from=1,to=30,by=2)</code></pre>
<pre><code>## [1] 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29</code></pre>
<pre class="r"><code>seq(from=1,length=50,by=.2)</code></pre>
<pre><code>## [1] 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6 2.8 3.0 3.2 3.4 3.6
## [15] 3.8 4.0 4.2 4.4 4.6 4.8 5.0 5.2 5.4 5.6 5.8 6.0 6.2 6.4
## [29] 6.6 6.8 7.0 7.2 7.4 7.6 7.8 8.0 8.2 8.4 8.6 8.8 9.0 9.2
## [43] 9.4 9.6 9.8 10.0 10.2 10.4 10.6 10.8</code></pre>
<p>The fifth argument is named along=vector, which is normally used as the only argument to create the sequence 1,2,…,length(vector), or the empty sequence if the vector is empty.</p>
<pre class="r"><code>#generates sequence from 1 to 30 as the length of backward_sequence is equal to 30
seq(from=1, along=backward_sequence)</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30</code></pre>
<p>A related function is <code>rep()</code> which can be used for replicating an object in various ways.</p>
<pre class="r"><code>#replicate dept_lecture_code vector 5 times
rep(dept_lecture_code,times=5)</code></pre>
<pre><code>## [1] 901 580 901 580 901 580 901 580 901 580</code></pre>
<pre class="r"><code>#replicate each element of dept_lecture_code vector before moving on to the next
rep(dept_lecture_code,each=5)</code></pre>
<pre><code>## [1] 901 901 901 901 901 580 580 580 580 580</code></pre>
</div>
<div id="logical-vectors" class="section level4">
<h4>Logical Vectors</h4>
<p>As well as numerical vectors, R allows manipulation of logical quantities. The elements of a logical vector can have the values TRUE, FALSE, or NA (for ?not available?, see <a href="#Missing%20Values">Missing Values</a>). The first two are often abbreviated as T and F, respectively. Note that T and F are just variables which are set to TRUE and FALSE by default, but are not reserved for words and hence can be overwritten by the user. Hence, you should always use TRUE and FALSE.</p>
<p>Logical vectors are generated by conditions.</p>
<pre class="r"><code>#generating logical vector by condition
logical_vector=one_to_thirty>13
print(logical_vector)</code></pre>
<pre><code>## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
## [12] FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
## [23] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE</code></pre>
<p>As you can see, <code>logical_vector</code> is a vector with the same length as <code>one_to_thirty</code> with values FALSE corresponding to the elements of <code>one_to_thirty</code> where the condition is not met and TRUE where it is.</p>
<p>The logical operators are <code><</code>, <code><=</code>, <code>></code>, <code>>=</code>, <code>==</code> for exact equality and <code>!=</code> for inequality. In addition, if c1 and c2 are logical expressions, then <code>c1 & c2</code> is their intersection (?and?), <code>c1 | c2</code> is their union (?or?), and <code>!c1</code> is the negation of c1.</p>
<p>Logical vectors may be used in ordinary arithmetic, in which case they are coerced into numeric vectors, FALSE becoming 0 and TRUE becoming 1. However there are situations where logical vectors and their coerced numeric counterparts are not equivalent, for example see the next subsection.</p>
</div>
<div id="missing-values" class="section level4">
<h4>Missing Values</h4>
<p>In some cases the components of a vector may not be completely known. When an element or value is ?not available? or a ?missing value? in the statistical sense, a place within a vector may be reserved for it by assigning it the special value <code>NA</code>. In general any operation on an <code>NA</code> becomes an <code>NA</code>. The motivation for this rule is simply that if the specification of an operation is incomplete, the result cannot be known, hence, is not available.</p>
<p>The function <code>is.na(x)</code> gives a logical vector of the same size as x with value TRUE if and only if the corresponding element in x is NA.</p>
<pre class="r"><code>x=c(1:3,NA)
ind=is.na(x)
print(ind)</code></pre>
<pre><code>## [1] FALSE FALSE FALSE TRUE</code></pre>
<p>Notice that the logical expression <code>x == NA</code> is quite different from <code>is.na(x)</code> since NA is not really a value but a marker for a quantity that is not available. Thus <code>x == NA</code> is a vector of the same length as x all of whose values are NA as the logical expression itself is incomplete and hence undecidable.</p>
<p>Note that there is a second kind of ?missing? values which are produced by numerical computation, the so-called Not a Number, NaN, values. Examples are given below. They give NaN since the result cannot be defined.</p>
<pre class="r"><code>0/0</code></pre>
<pre><code>## [1] NaN</code></pre>
<pre class="r"><code>#or
Inf-Inf</code></pre>
<pre><code>## [1] NaN</code></pre>
<p>In summary, <code>is.na(x)</code> is TRUE both for NA and NaN values. To differentiate these, <code>is.nan(x)</code> is only TRUE for NaNs.</p>
<p>Missing values are sometimes printed as <code><NA></code> when character vectors are printed without quotes.</p>
</div>
<div id="character-vectors" class="section level4">
<h4>Character Vectors</h4>
<p>Character quantities and character vectors are used frequently in R, for example as plot labels. Where needed they are denoted by a sequence of characters delimited by the double quote character, e.g., “x-values”, “New iteration results”.</p>
<p>Character strings are entered using either matching double (“) or single (?) quotes, but are printed using double quotes (or sometimes without quotes). They use C-style escape sequences, using \ as the escape character, so \\ is entered and printed as \\, and inside double quotes” is entered as \". Other useful escape sequences are \n, newline, \t, tab and \b, backspace–see <code>?Quotes</code> for a full list.</p>
<p>Character vectors may be concatenated into a vector by the <code>c()</code> function; examples of their use will emerge frequently.</p>
<p>The <code>paste()</code> function takes an arbitrary number of arguments and concatenates them one by one into character strings. Any numbers given among the arguments are coerced into character strings in the evident way, that is, in the same way they would be if they were printed. The arguments are by default separated in the result by a single blank character, but this can be changed by the named argument, <code>sep=string</code>, which changes it to string, possibly empty.</p>
<pre class="r"><code>labs=paste(c("X","Y"),1:10,sep="")
print(labs)</code></pre>
<pre><code>## [1] "X1" "Y2" "X3" "Y4" "X5" "Y6" "X7" "Y8" "X9" "Y10"</code></pre>
<p>Note particularly that recycling of short lists takes place here too; thus <code>c("X", "Y")</code> is repeated 5 times to match the sequence 1:10.</p>
</div>
<div id="index-vectors" class="section level4">
<h4>Index Vectors</h4>
<p>Subsets of the elements of a vector may be selected by appending to the name of the vector an index vector in square brackets. More generally any expression that evaluates to a vector may have subsets of its elements similarly selected by appending an index vector in square brackets immediately after the expression.</p>
<p>Such index vectors can be any of four distinct types.</p>
<ol style="list-style-type: decimal">
<li><strong>A logical vector:</strong> In this case the index vector is recycled to the same length as the vector from which elements are to be selected. Values corresponding to TRUE in the index vector are selected and those corresponding to FALSE are omitted.</li>
</ol>
<pre class="r"><code>#a vector containing non-missing values of one_to_thirty
y=one_to_thirty[!is.na(one_to_thirty)]</code></pre>
<p>Note that if one_to_thirty has missing values, y will be shorter than one_to_thirty.</p>
<pre class="r"><code>#a vector containing the values of one_to_thirty which are non-missing and positive
z=(one_to_thirty+1)[(!is.na(one_to_thirty))&one_to_thirty>0]</code></pre>
<ol start="2" style="list-style-type: decimal">
<li><strong>A vector of positive integral quantities:</strong> In this case the values in the index vector must lie in the set <code>{1, 2, . . . , length(x)}</code>. The corresponding elements of the vector are selected and concatenated, in that order, in the result. The index vector can be of any length and the result is of the same length as the index vector. For example <code>x[6]</code> is the sixth component of x.</li>
</ol>
<pre class="r"><code>#select the first 10 elements
one_to_thirty[1:10]</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10</code></pre>
<ol start="3" style="list-style-type: decimal">
<li><strong>A vector of negative integral quantities:</strong> Such an index vector specifies the values to be excluded rather than included.</li>
</ol>
<pre class="r"><code>#discard the first 5 elements
one_to_thirty[-(1:5)]</code></pre>
<pre><code>## [1] 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
## [24] 29 30</code></pre>
<ol start="4" style="list-style-type: decimal">
<li><strong>A vector of character strings:</strong> This possibility only applies where an object has a names attribute to identify its components. In this case a sub-vector of the names vector may be used in the same way as the positive integral labels in item 2 further above.</li>
</ol>
<pre class="r"><code>fruit=c(5,10,1,20)
names(fruit)=c("orange","banana","apple","peach")
lunch=fruit[c("apple","orange")]
print(lunch)</code></pre>
<pre><code>## apple orange
## 1 5</code></pre>
<p>The advantage is that alphanumeric names are often easier to remember than numeric indices. This option is particularly useful in connection with data frames, as we will see later.</p>
<p>An indexed expression can also appear on the receiving end of an assignment, in which case the assignment operation is performed only on those elements of the vector. The expression must be of the form <code>vector[index_vector]</code> as having an arbitrary expression in place of the vector name does not make much sense here.</p>
<pre class="r"><code>#replace missing values with zeros
one_to_thirty[is.na(one_to_thirty)]=0</code></pre>
</div>
</div>
<div id="matrices-and-arrays" class="section level3">
<h3>Matrices and Arrays</h3>
<p>Arrays are data n-dimensional data structures that hold the data of the same type. Arrays have dimension <code>(dim)</code> attribute. Matrices are two-dimensional arrays.</p>
<pre class="r"><code>#define an array
z=array(1:24,dim=c(3,4,2))
print(z)</code></pre>
<pre><code>## , , 1
##
## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
##
## , , 2
##
## [,1] [,2] [,3] [,4]
## [1,] 13 16 19 22
## [2,] 14 17 20 23
## [3,] 15 18 21 24</code></pre>
<div id="indexing" class="section level4">
<h4>Indexing</h4>
<p>Individual elements of an array may be referenced by giving the name of the array followed by the subscripts in square brackets, separated by commas.</p>
<p>More generally, subsections of an array may be specified by giving a sequence of index vectors in place of subscripts; however if any index position is given an empty index vector, then the full range of that subscript is taken.</p>
<p>Continuing the previous example, <code>z[2,,]</code> is a 4x2 array with dimension vector <code>c(4,2)</code>.</p>
<pre class="r"><code>#values of the z[2,,] array
c(z[2,1,1], z[2,2,1], z[2,3,1], z[2,4,1], z[2,1,2], z[2,2,2], z[2,3,2], z[2,4,2])</code></pre>
<pre><code>## [1] 2 5 8 11 14 17 20 23</code></pre>
<p><code>z[,,]</code> stands for the entire array which is the same as omitting the subscripts entirely and using <code>z</code> alone.</p>
<p>If an array name is given with just one subscript or index vector, then the corresponding values of the data vector only are used; in this case the dimension vector is ignored. This is not the case, however, if the single index is not a vector but itself an array, as we next discuss.</p>
</div>
<div id="index-matrices" class="section level4">
<h4>Index Matrices</h4>
<p>As well as an index vector in any subscript position, a matrix may be used with a single index matrix in order either to assign a vector of quantities to an irregular collection of elements in the array, or to extract an irregular collection as a vector.</p>
<p>A matrix example makes it clear. In the case of a doubly indexed array, an index matrix may be given consisting of two columns and as many rows as desired. The entries in the index matrix are the row and column indices for the doubly indexed array. Suppose for example we have a 4 by 5 array X and we wish to do the following:</p>
<ul>
<li>Extract elements X[1,3], X[2,2] and X[3,1] as a vector</li>
<li>Replace those entries with zeros in the array of X</li>
</ul>
<pre class="r"><code>x=array(1:20,dim=c(4,5)) #generate a 4x5 array
x</code></pre>
<pre><code>## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20</code></pre>
<pre class="r"><code>i=array(c(1:3,3:1),dim=c(3,2)) #i is a 3x2 index array
i</code></pre>
<pre><code>## [,1] [,2]
## [1,] 1 3
## [2,] 2 2
## [3,] 3 1</code></pre>
<pre class="r"><code>#extract those elements from x
x[i]</code></pre>
<pre><code>## [1] 9 6 3</code></pre>
<pre class="r"><code>#replace the elements selected with zeros
x[i]=0
x</code></pre>
<pre><code>## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 0 13 17
## [2,] 2 0 10 14 18
## [3,] 0 7 11 15 19
## [4,] 4 8 12 16 20</code></pre>
<p>Negative indices are not allowed in index matrices. NA and zero values are allowed: rows in the index matrix containing a zero are ignored, and rows containing an NA produce an NA in the result.</p>
</div>
<div id="matrix-multiplication" class="section level4">
<h4>Matrix Multiplication</h4>
<p>The operator %*% is used for matrix multiplication. An n by 1 or 1 by n matrix may of course be used as an n-vector if in the context such is appropriate. Conversely, vectors which occur in matrix multiplication expressions are automatically promoted either to row or column vectors, whichever is multiplicatively coherent, if possible.</p>
<pre class="r"><code>#A and B are the square matrices of the same size
A=matrix(1:4,nrow=2,ncol=2)
B=matrix(5:8,nrow=2,ncol=2)
#element by element products
A*B</code></pre>
<pre><code>## [,1] [,2]
## [1,] 5 21
## [2,] 12 32</code></pre>
<pre class="r"><code>#matrix product
A%*%B</code></pre>
<pre><code>## [,1] [,2]
## [1,] 23 31
## [2,] 34 46</code></pre>
<p>The function <code>crossprod()</code> forms ?cross products?, meaning that <code>crossprod(A, B)</code> is the same as <code>t(A) %*% B</code> but the operation is more efficient. If the second argument of <code>crossprod()</code> is omitted, it is taken to be the same as the first.</p>
<pre class="r"><code>#cross products of A and B with crossprod function
crossprod(A,B)</code></pre>
<pre><code>## [,1] [,2]
## [1,] 17 23
## [2,] 39 53</code></pre>
<p>The meaning of <code>diag()</code> depends on its argument. <code>diag(v)</code>, where v is a vector, gives a diagonal matrix with elements of the vector as the diagonal entries. On the other hand <code>diag(M)</code>, where M is a matrix, gives the vector of main diagonal entries of M. Also, somewhat confusingly, if k is a single numeric value then <code>diag(k)</code> is the k by k identity matrix!</p>
<pre class="r"><code>#diagonal matrix of a vector
diag(dept_lecture_code)</code></pre>
<pre><code>## [,1] [,2]
## [1,] 901 0
## [2,] 0 580</code></pre>
<pre class="r"><code>#diagonal elements of a matrix
diag(A)</code></pre>
<pre><code>## [1] 1 4</code></pre>
<pre class="r"><code>#diag function with numeric value
d=2
diag(d) #produces identity matrix with dimension d</code></pre>
<pre><code>## [,1] [,2]
## [1,] 1 0
## [2,] 0 1</code></pre>
</div>
<div id="combining-matrices" class="section level4">
<h4>Combining Matrices</h4>
<p>In R, <code>cbind()</code> forms matrices by binding together matrices horizontally, or column-wise, and <code>rbind()</code> vertically, or row-wise. The arguments to <code>cbind()</code> must be either vectors of any length, or matrices with the same column size, that is the same number of rows.</p>
<pre class="r"><code>#column-wise binding
cbind(A,B)</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 3 5 7
## [2,] 2 4 6 8</code></pre>
<pre class="r"><code>#row-wise binding
rbind(A,B)</code></pre>
<pre><code>## [,1] [,2]
## [1,] 1 3
## [2,] 2 4
## [3,] 5 7
## [4,] 6 8</code></pre>
</div>
</div>
<div id="lists" class="section level3">
<h3>Lists</h3>
<p>Lists are the R objects which contain elements of different types like − numbers, strings, vectors and another list inside it. A list can also contain a matrix or a function as its elements.</p>
<pre class="r"><code>x <- list("a" = 2.5, "b" = TRUE, "c" = 1:3)
str(x)</code></pre>
<pre><code>## List of 3
## $ a: num 2.5
## $ b: logi TRUE
## $ c: int [1:3] 1 2 3</code></pre>
<div id="accessing-elements-in-a-list" class="section level4">
<h4>Accessing elements in a list</h4>
<p>Lists can be accessed in similar fashion to vectors. Integer, logical or character vectors can be used for indexing. Followings are some of the different methods to accessing an element in the list.</p>
<pre class="r"><code>x["a"] # give us a sublist not the content inside</code></pre>
<pre><code>## $a
## [1] 2.5</code></pre>
<pre class="r"><code>x[["a"]] # to retrieve the content</code></pre>
<pre><code>## [1] 2.5</code></pre>
<pre class="r"><code>x$a # same as x[["a"]]</code></pre>
<pre><code>## [1] 2.5</code></pre>
<p>An R list is an object consisting of an ordered collection of objects known as its components.</p>
<p>There is no particular need for the components to be of the same mode or type, and, for example, a list could consist of a numeric vector, a logical value, a matrix, a complex vector, a character array, a function, and so on. Here is a simple example of how to make a list:</p>
<pre class="r"><code>#A list
family=list(name="Fred",wife="Mary",noChildren=3,childAges=c(4,7,9))</code></pre>
<p>Components are always numbered and may always be referred to as such. Thus if <code>family</code> is the name of a list with four components, these may be individually referred to as</p>
<pre class="r"><code>#Components of a list
family[[1]]</code></pre>
<pre><code>## [1] "Fred"</code></pre>
<pre class="r"><code>family[[2]]</code></pre>
<pre><code>## [1] "Mary"</code></pre>
<pre class="r"><code>family[[3]]</code></pre>
<pre><code>## [1] 3</code></pre>
<pre class="r"><code>family[[4]]</code></pre>
<pre><code>## [1] 4 7 9</code></pre>
<pre class="r"><code>family[[4]][[2]] #gives the second element of the 4th component</code></pre>
<pre><code>## [1] 7</code></pre>
<pre class="r"><code>length(family) #the number of components of the list</code></pre>
<pre><code>## [1] 4</code></pre>
<p>Components of lists may also be named, and in this case the component may be referred to either by giving the component name as a character string in place of the number in double square brackets, or, more conveniently, by giving an expression of the form <code>name$component_name</code></p>
<pre class="r"><code>#the expressions below give the same result
family$name #calling with component name</code></pre>
<pre><code>## [1] "Fred"</code></pre>
<pre class="r"><code>family[[1]] #calling with index</code></pre>
<pre><code>## [1] "Fred"</code></pre>
<pre class="r"><code>#calling the vector elements in the list
family$childAges[1]</code></pre>
<pre><code>## [1] 4</code></pre>
<pre class="r"><code>family[[4]][[1]]</code></pre>
<pre><code>## [1] 4</code></pre>
</div>
</div>
<div id="data-frame" class="section level3">
<h3>Data Frame</h3>
<p>A data frame is a list with class “data.frame”. There are restrictions on lists that may be made into data frames, namely</p>
<ul>
<li>The components must be vectors (numeric, character, or logical), factors, numeric matrices, lists, or other data frames.</li>
<li>Matrices, lists, and data frames provide as many variables to the new data frame as they have columns, elements, or variables, respectively.</li>
<li>Numeric vectors, logicals and factors are included as is, and by default1 character vectors are coerced to be factors, whose levels are the unique values appearing in the vector.</li>
<li>Vector structures appearing as variables of the data frame must all have the same length, and matrix structures must all have the same row size.</li>
</ul>
<p>A data frame may for many purposes be regarded as a matrix with columns possibly of differing modes and attributes. It may be displayed in matrix form, and its rows and columns extracted using matrix indexing conventions.</p>
<p>A data frame is a table or a two-dimensional array-like structure in which each column contains values of one variable and each row contains one set of values from each column.</p>
<div id="creating-a-data-frame-from-vectors" class="section level4">
<h4>Creating a data frame from vectors</h4>
<pre class="r"><code>employee <- c('John Doe','Peter Gynn','Jolie Hope') #vector
salary <- c(21000, 23400, 26800) #vector
startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14')) #vector
data = data.frame(employee, salary, startdate) # data frame
data</code></pre>
<pre><code>## employee salary startdate
## 1 John Doe 21000 2010-11-01
## 2 Peter Gynn 23400 2008-03-25
## 3 Jolie Hope 26800 2007-03-14</code></pre>
<pre class="r"><code># A data frame
codes_names_df = data.frame(dept_lecture_code, dept_lecture_name)</code></pre>
</div>
<div id="reading-data-from-files" class="section level4">
<h4>Reading Data from Files</h4>
<p>Large data objects will usually be read as values from external files rather than entered during an R session at the keyboard. R input facilities are simple and their requirements are fairly strict and even rather inflexible. There is a clear presumption by the designers of R that you will be able to modify your input files using other tools, such as file editors or Perl1 to fit in with the requirements of R. Generally this is very simple.</p>
<p>If variables are to be held mainly in data frames, as we strongly suggest they should be, an entire data frame can be read directly with the <code>read.table()</code> function. There is also a more primitive input function, <code>scan()</code>, that can be called directly.</p>
<p>One of the main methods to read a data from files is to use built-in <code>read.csv</code> method. It basically reads the .csv files. You can set whether to collect the header (the first row in the .csv file) or the line seperator in the file as options.</p>
<pre class="r"><code>data = read.csv("cars.csv", header = T, sep = ";")
head(data)</code></pre>
<pre><code>## Car MPG Cylinders Displacement Horsepower
## 1 STRING DOUBLE INT DOUBLE DOUBLE
## 2 Chevrolet Chevelle Malibu 18.0 8 307.0 130.0
## 3 Buick Skylark 320 15.0 8 350.0 165.0
## 4 Plymouth Satellite 18.0 8 318.0 150.0
## 5 AMC Rebel SST 16.0 8 304.0 150.0
## 6 Ford Torino 17.0 8 302.0 140.0
## Weight Acceleration Model Origin
## 1 DOUBLE DOUBLE INT CAT
## 2 3504. 12.0 70 US
## 3 3693. 11.5 70 US
## 4 3436. 11.0 70 US
## 5 3433. 12.0 70 US
## 6 3449. 10.5 70 US</code></pre>
<p>For more details on importing data into R and also exporting data, see the R Data Import/Export manual.</p>
</div>
<div id="accessing-built-in-data-sets" class="section level4">
<h4>Accessing built-in data sets</h4>
<p>Around 100 datasets are supplied with R (in package datasets), and others are available in packages (including the recommended packages supplied with R). To see the list of datasets currently available use <code>data()</code>.</p>
<pre class="r"><code># Iris is an existing dataset in R. You can load it directly
data(iris)
str(iris)</code></pre>
<pre><code>## 'data.frame': 150 obs. of 5 variables:
## $ Sepal.Length: num 5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
## $ Sepal.Width : num 3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
## $ Petal.Length: num 1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
## $ Petal.Width : num 0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
## $ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...</code></pre>
<p><code>str</code> function outputs the names, types and first few entries of a data frame. This allows you to take a quick look at the data. As you can see, ‘Species’ variable in the data frame is a new data type called “Factor”. This is essentially a character variable with known amount of different values. This allows R to run count-type statistics on it. Most character variables are represented as factors by default when loading into data frames unless explicitly specified. We will talk more about factors later.</p>
</div>
</div>
<div id="basic-descriptive-statistics" class="section level3">
<h3>Basic Descriptive Statistics</h3>
<p>Summary function outputs basic statistics regarding the data frame. For numeric variables, these statistics describe the five-point statistics. For factor variables, this is of count-type. If we represented our character variable as character type instead of factor, that would be impossible.</p>
<pre class="r"><code>iris$Species = as.character(iris$Species)
summary(iris)</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## Length:150
## Class :character
## Mode :character
##
##
## </code></pre>
<p>I did two things in the previous chunk. First, I accessed the <code>Species</code> variable, and casted it into type character. Then, I took the summary of the data frame once more. <code>$</code> operator is the simplest way to index a variable in R. However it’s not fool-proof. The recommended way to index a variable is as follows, with double brackets and quote-marked-variable-names:</p>
<pre class="r"><code>data(iris) # Reload original iris data
iris[["Species"]] = as.character(iris[["Species"]])
summary(iris)</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## Length:150
## Class :character
## Mode :character
##
##
## </code></pre>
<p>You can perform mathematical computations on numeric variables and create new ones using the same syntax.</p>
<pre class="r"><code>iris[["New_Numeric_Var_1"]] = iris[["Sepal.Length"]] + iris[["Sepal.Width"]]
iris$New_Numeric_Var_2 = iris$Sepal.Length/(iris$Sepal.Width+0.00001)</code></pre>
<p>In the first line, I indexed the variables using the prefered method and made a new variable. In the second line, I used the simple method and made a new variable. I also added a scalar to all values in <code>Sepal.Width</code> variable. R is a very flexible language. You can do most computations without explicitly casting the data into the same dimension, however this may cause issues. I won’t demonstrate this for now, but make sure to regularly check your data for any inconsistencies that this flexibility might cause.</p>
</div>
</div>
<div id="save-load-environment-objects-in-r" class="section level2">
<h2>Save / Load Environment Objects in R</h2>
<p>You can serialize and save any object or even the whole environment in R. For this, you can use <code>save.image</code> function.</p>
<p>After we had created some objects into the R environment, you can use it to save the entire list of environment objects to a file. By using <code>load</code> function you can load back the saved environment.</p>
<pre class="r"><code>save.image(file='myEnvironment.RData')
load('myEnvironment.RData')</code></pre>
</div>
<div id="installing-packages" class="section level2">
<h2>Installing Packages</h2>
<p>For future tasks, you will need to install different packages. To perform this installation, either select “Install Packages” from “Tools” menu in RStudio, or use the <code>install.packages</code> command in console. Let’s install <code>stringr</code> package that you will be using in the class using this command.</p>
<pre class="r"><code># install.packages("stringr")
# I have this package already installed so I'm skipping this. You should run this command.</code></pre>
<p>R also has control structures that any programming language has so let’s try to install this package with an ‘if’ structure.</p>
<pre class="r"><code>if(!"stringr"%in%installed.packages()){
install.packages("stringr")
}</code></pre>
<p>What this chunk does is, checks whether <code>stringr</code> is installed and installs it if it’s not installed. <code>installed.packages()</code> command lists all the installed packages in your default libraries. <code>%in%</code> expression checks whether the left hand side is in the right hand side and ‘!’ notation is negation in R. As before, ‘install.packages’ command installs the package. There are for loops, while loops, try-catch structures etc. in R. We can talk about these when the need arises. If you want to see more examples on control structures, go to <a href="https://www.r-bloggers.com/control-structures-loops-in-r/">one of</a> the several tutorials available online.</p>
</div>
<div id="basic-data-wrangling-with-dplyr" class="section level2">
<h2>Basic Data Wrangling with <code>dplyr</code></h2>
<p>The dplyr package makes these steps fast and easy:</p>
<ul>
<li>By constraining your options, it helps you think about your data manipulation challenges.</li>
<li>It provides simple “verbs”, functions that correspond to the most common data manipulation tasks, to help you translate your thoughts into code.</li>
<li>It uses efficient backends, so you spend less time waiting for the computer.</li>
</ul>
<p><code>%>%</code> is called pipe. By piping each function in the dplyr package, you can make various analysis.</p>
<p>Below, we present some of the basic things you can do with it. For more, please refer to the <a href="https://cran.r-project.org/web/packages/dplyr/vignettes/dplyr.html">official dplyr documentation</a>.</p>
<pre class="r"><code>library(dplyr)</code></pre>
<pre><code>##
## Attaching package: 'dplyr'</code></pre>
<pre><code>## The following objects are masked from 'package:stats':
##
## filter, lag</code></pre>
<pre><code>## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union</code></pre>
<pre class="r"><code>head(iris %>% select(Sepal.Length, Sepal.Width)) # Selects only 2 columns in the data frame.</code></pre>
<pre><code>## Sepal.Length Sepal.Width
## 1 5.1 3.5
## 2 4.9 3.0
## 3 4.7 3.2
## 4 4.6 3.1
## 5 5.0 3.6
## 6 5.4 3.9</code></pre>
<pre class="r"><code>iris %>% filter(Sepal.Length < 5, Sepal.Width > 3.5) # You can filter the data frame.</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 4.6 3.6 1.0 0.2 setosa
## 2 4.9 3.6 1.4 0.1 setosa
## New_Numeric_Var_1 New_Numeric_Var_2
## 1 8.2 1.277774
## 2 8.5 1.361107</code></pre>
<pre class="r"><code>iris %>% rowwise() %>% summarise(Sepal.Area = Sepal.Length*Sepal.Width) # You can create new features</code></pre>
<pre><code>## # A tibble: 150 x 1
## Sepal.Area
## <dbl>
## 1 17.8
## 2 14.7
## 3 15.0
## 4 14.3
## 5 18
## 6 21.1
## 7 15.6
## 8 17
## 9 12.8
## 10 15.2
## # … with 140 more rows</code></pre>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open')
});
});
</script>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>