-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.html
4926 lines (4181 loc) · 133 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>
<head>
<title>Intro to R Workshop</title>
<meta charset="utf-8">
<meta name="description" content="Intro to R Workshop">
<meta name="author" content="Sepehr Akhavan, Homer Strong, Emily Smith, Eric Lai">
<meta name="generator" content="slidify" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<link rel="stylesheet" href="libraries/frameworks/io2012/css/default.css" media="all" >
<link rel="stylesheet" href="libraries/frameworks/io2012/css/phone.css"
media="only screen and (max-device-width: 480px)" >
<link rel="stylesheet" href="libraries/frameworks/io2012/css/slidify.css" >
<link rel="stylesheet" href="libraries/highlighters/highlight.js/css/tomorrow.css" />
<base target="_blank"> <!-- This amazingness opens all links in a new tab. --> <link rel=stylesheet href="./assets/css/logo.css"></link>
<link rel=stylesheet href="./assets/css/ribbons.css"></link>
<!-- Grab CDN jQuery, fall back to local if offline -->
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.min.js"></script>
<script>window.jQuery || document.write('<script src="libraries/widgets/quiz/js/jquery.js"><\/script>')</script>
<script data-main="libraries/frameworks/io2012/js/slides"
src="libraries/frameworks/io2012/js/require-1.0.8.min.js">
</script>
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- LOGO SLIDE -->
<slide class="title-slide segue nobackground">
<aside class="gdbar">
<img src="assets/img/logo.png">
</aside>
<hgroup class="auto-fadein">
<h1>Intro to R Workshop</h1>
<h2>UCI Data Science Initiative</h2>
<p>Sepehr Akhavan, Homer Strong, Emily Smith, Eric Lai<br/>Dept. of Statistics</p>
</hgroup>
<a href="https://github.com/UCIDataScienceInitiative/IntroR_Workshop/zipball/gh-pages" class="example">
Download
</a>
<article></article>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<hgroup>
<h2>Introduction</h2>
</hgroup>
<article data-timings="">
<p>1) The class will include 5 sessions: </p>
<ul>
<li>Session 1 (9-10:20): Data Types in R </li>
<li>Session 2 (10:30-11:20): Control Structures and Functions</li>
<li>Session 3 (11:30-12): Statistical Distributions in R</li>
<li>Exercise 1 (12:30-1:20): Basic Data Exploration</li>
<li>Session 4 (1:20-2:50): Statistical Analysis in R </li>
<li>Session 5 (3:00-4:20): Plotting and Data Visualization in R</li>
<li>Exercise 2 (4:20-5:00): Data visualization & Statistical Analysis</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>Introduction</h2>
</hgroup>
<article data-timings="">
<p>2) We are going to work in pairs. Please find a partner. </p>
<p>3) Feel free to ask questions anytime during lectures.</p>
<p>4) To access this presentation and the codes used during the workshop please visit:</p>
<ul>
<li><a href="http://ucidatascienceinitiative.github.io/IntroR_Workshop/#1">http://ucidatascienceinitiative.github.io/IntroR_Workshop/#1</a></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>Session 1 - Agenda</h2>
</hgroup>
<article data-timings="">
<ol>
<li>RStudio</li>
<li>Data Types in R</li>
<li>Subsetting in R</li>
</ol>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-4" style="background:;">
<hgroup>
<h2>What is R?</h2>
</hgroup>
<article data-timings="">
<ul>
<li><p>R is a free software environment for statistical computing and graphics</p>
<ul>
<li>See <a href="http://www.r-project.org/">http://www.r-project.org/</a> for more info</li>
</ul></li>
<li><p>R compiles and runs on a wide variety of UNIX platforms, Windows and Mac OS</p></li>
<li><p>R is Open-Source and free</p></li>
<li><p>R is fundamentally a command-driven system</p></li>
<li><p>R is an object-oriented programming language </p>
<ul>
<li>everything in R is considered as an object!</li>
</ul></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-5" style="background:;">
<hgroup>
<h2>R Studio:</h2>
</hgroup>
<article data-timings="">
<ol>
<li><p>RStudio is a free and open source integrated development environment (IDE) for R.</p></li>
<li><p>To download RStudio please visit: <a href="http://rstudio.org/">http://rstudio.org/</a></p></li>
<li><p>Please note that you must have R already installed before installing R Studio.</p></li>
</ol>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-6" style="background:;">
<hgroup>
<h2>Data Types in R:</h2>
</hgroup>
<article data-timings="">
<ol>
<li><p>R has 5 main atomic data types:</p>
<ul>
<li>Numeric</li>
<li>Integer</li>
<li>Complex</li>
<li>Logical</li>
<li>Character</li>
</ul></li>
<li><p>Everything in R is object. Objects can have some attributes.</p>
<ul>
<li>names, dimension, length are some possible attributes</li>
</ul></li>
</ol>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background:;">
<hgroup>
<h2>Vectors in R:</h2>
</hgroup>
<article data-timings="">
<p>Vector is the most basic object in R</p>
<pre><code class="r">numVec <- 1:10 # <- : is assigning operator
numVec
</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10
</code></pre>
<pre><code class="r">charVec <- c("a", "b", "c") # c: to combine elements
charVec
</code></pre>
<pre><code>## [1] "a" "b" "c"
</code></pre>
<pre><code class="r">logVec <- vector(mode = "logical", length = 10)
logVec
</code></pre>
<pre><code>## [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-8" style="background:;">
<hgroup>
<h3>Special Values:</h3>
</hgroup>
<article data-timings="">
<p>There are some special values in R:</p>
<ul>
<li>use L to refer to an integer value: 1L</li>
<li>R knows infinity: Inf, -Inf</li>
<li>NaN: refers to "Not a number"</li>
</ul>
<pre><code class="r">intVec <- c(1L, 2L, 3L, 4L)
intVec
</code></pre>
<pre><code>## [1] 1 2 3 4
</code></pre>
<pre><code class="r">a <- Inf; b <- 0
rslt <- c(b/a, a/a)
rslt
</code></pre>
<pre><code>## [1] 0 NaN
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h3>Logical, Complex, & Character Vectors:</h3>
</hgroup>
<article data-timings="">
<p>Let's see some examples of logical, complex, and character vectors:</p>
<pre><code class="r">logVec <- c(TRUE, FALSE, FALSE, T, F)
logVec
</code></pre>
<pre><code>## [1] TRUE FALSE FALSE TRUE FALSE
</code></pre>
<pre><code class="r">compVec <- c(1 + 0i, 3 + 1i)
compVec
</code></pre>
<pre><code>## [1] 1+0i 3+1i
</code></pre>
<pre><code class="r">charVec <- c("red", "green", "blue")
charVec
</code></pre>
<pre><code>## [1] "red" "green" "blue"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-10" style="background:;">
<hgroup>
<h3>Data Type Coercion:</h3>
</hgroup>
<article data-timings="">
<ul>
<li>In general, vectors CAN NOT have mixed types of objects</li>
<li>exception: lists in R </li>
</ul>
<pre><code class="r">numCharVec <- c(3.14, "a")
numCharVec # ? what would you expect to be printed?
numLogVec <- c(pi, T)
numLogVec # any guess?
charLogVec <- c("a", TRUE)
charLogVec # ?
</code></pre>
<ul>
<li>In examples above, we saw implicit coercion </li>
<li>Explicit coercion is also possible!</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-11" style="background:;">
<hgroup>
<h3>Data Type Coercion:</h3>
</hgroup>
<article data-timings="">
<ul>
<li>as(): To explicitly coerce objects from one type to another</li>
</ul>
<pre><code class="r">numVec <- seq(from = 1200, to = 1300, by = 15)
numVec
</code></pre>
<pre><code>## [1] 1200 1215 1230 1245 1260 1275 1290
</code></pre>
<pre><code class="r">numToChar <- as(numVec, "character")
numToChar
</code></pre>
<pre><code>## [1] "1200" "1215" "1230" "1245" "1260" "1275" "1290"
</code></pre>
<pre><code class="r">logVec <- c(F, T, F, T, T)
as(logVec, "numeric")
</code></pre>
<pre><code>## [1] 0 1 0 1 1
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background:;">
<hgroup>
<h3>Data Type Coercion:</h3>
</hgroup>
<article data-timings="">
<ul>
<li>Coercion does not always work! Be careful about warnings:</li>
</ul>
<pre><code class="r">compVec <- c(12+10i, 1+6i, -3-2i)
as(compVec, "numeric")
</code></pre>
<pre><code>## Warning: imaginary parts discarded in coercion
</code></pre>
<pre><code>## [1] 12 1 -3
</code></pre>
<pre><code class="r">charVec <- c("2.5", "3", "2.8", "1.5", "zero")
as(charVec, "numeric")
</code></pre>
<pre><code>## Warning: NAs introduced by coercion
</code></pre>
<pre><code>## [1] 2.5 3.0 2.8 1.5 NA
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background:;">
<hgroup>
<h3>Factors:</h3>
</hgroup>
<article data-timings="">
<ul>
<li>Factor is a vector object used to specify a discrete classification (categorical values).</li>
<li>Factors can be: 1) ordered, 2) un-ordered</li>
<li>Levels of a Factor are better to be labeled (self-descriptive)
<ul>
<li>Consider gender as (0, 1) as opposed to labeled ("F", "M")</li>
</ul></li>
</ul>
<pre><code class="r">Gender <- rep(c("Female", "Male"), times = 3)
Gender
</code></pre>
<pre><code>## [1] "Female" "Male" "Female" "Male" "Female" "Male"
</code></pre>
<pre><code class="r">GenderFac1 <- factor(Gender)
GenderFac1
</code></pre>
<pre><code>## [1] Female Male Female Male Female Male
## Levels: Female Male
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background:;">
<hgroup>
<h3>Factors:</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">levels(GenderFac1)
</code></pre>
<pre><code>## [1] "Female" "Male"
</code></pre>
<pre><code class="r">table(GenderFac1)
</code></pre>
<pre><code>## GenderFac1
## Female Male
## 3 3
</code></pre>
<pre><code class="r">unclass(GenderFac1) # bring the factor down to integer values
</code></pre>
<pre><code>## [1] 1 2 1 2 1 2
## attr(,"levels")
## [1] "Female" "Male"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15" style="background:;">
<hgroup>
<h3>Factors:</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">GenderFac1 # levels are ordered alphabetically - 1st level = BaseLevel
</code></pre>
<pre><code>## [1] Female Male Female Male Female Male
## Levels: Female Male
</code></pre>
<pre><code class="r">GenderFac2 <- factor(Gender, levels = c("Male", "Female"))
GenderFac1
</code></pre>
<pre><code>## [1] Female Male Female Male Female Male
## Levels: Female Male
</code></pre>
<pre><code class="r">GenderFac2
</code></pre>
<pre><code>## [1] Female Male Female Male Female Male
## Levels: Male Female
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-16" style="background:;">
<hgroup>
<h3>Missing Values:</h3>
</hgroup>
<article data-timings="">
<ul>
<li><p>There are two kinds of missing values in R:</p>
<ul>
<li>NaN: refers to "Not a Number" and is a a missing value produced by numerical computation.</li>
<li>NA: When a value is "Not Available" or is "Missing", NA is assigned as its value.</li>
</ul></li>
<li><p>NaN is also considered as NA (the reverse is NOT true). </p></li>
</ul>
<pre><code class="r">testScore <- NA
is.na(testScore)
</code></pre>
<pre><code>## [1] TRUE
</code></pre>
<pre><code class="r">is.nan(testScore)
</code></pre>
<pre><code>## [1] FALSE
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-17" style="background:;">
<hgroup>
<h3>Matrices:</h3>
</hgroup>
<article data-timings="">
<ul>
<li>Matrix is a special case of vector:
<ul>
<li>Matrix has dimension attribute</li>
</ul></li>
</ul>
<pre><code class="r">myMat <- matrix(nrow = 2, ncol = 4)
myMat
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] NA NA NA NA
## [2,] NA NA NA NA
</code></pre>
<pre><code class="r">attributes(myMat)
</code></pre>
<pre><code>## $dim
## [1] 2 4
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background:;">
<hgroup>
<h3>Matrices:</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">myMat <- matrix(1:8, nrow = 2, ncol = 4)
myMat # matrices are filled in column-wise
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 3 5 7
## [2,] 2 4 6 8
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-19" style="background:;">
<hgroup>
<h3>Matrix is a special vector:</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">myVec <- 1:8
myVec
</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8
</code></pre>
<pre><code class="r">dim(myVec) <- c(2,4)
myVec
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 3 5 7
## [2,] 2 4 6 8
</code></pre>
<ul>
<li>Similar to vectors, all elements of a matrix should have the same type.
<ul>
<li>if not, R does an automatic coercion.</li>
</ul></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-20" style="background:;">
<hgroup>
<h3>Other Ways to Create Matrix:</h3>
</hgroup>
<article data-timings="">
<ul>
<li><p>As it's intuitive, matrices seem to be a combination of vectors that are put next to each other (either column wise or row wise).</p></li>
<li><p>rbind() (row bind) and cbind (column bind) do a similar job:</p></li>
</ul>
<pre><code class="r">vec1 <- 1:4
vec2 <- sample(1:100, 4, replace = FALSE)
vec3 <- rnorm(4, mean = 0, sd = 1)
colMat <- cbind(vec1, vec2, vec3)
colMat
</code></pre>
<pre><code>## vec1 vec2 vec3
## [1,] 1 88 -0.60388
## [2,] 2 64 -0.07985
## [3,] 3 74 0.89368
## [4,] 4 55 1.85324
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-21" style="background:;">
<hgroup>
<h3>Other Ways to Create Matrix:</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">vec1 <- 1:4
vec2 <- sample(1:100, 4, replace = FALSE)
vec3 <- rnorm(4, mean = 0, sd = 1)
rowMat <- rbind(vec1, vec2, vec3)
rowMat
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## vec1 1.0000 2.0000 3.000 4.00
## vec2 21.0000 62.0000 74.000 9.00
## vec3 0.6717 0.1932 -2.489 1.09
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-22" style="background:;">
<hgroup>
<h2>Lists:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>Consider list as a vector but with two main differences:
<ul>
<li>each element of a list can have its own class regardless of other elements</li>
<li>This means, each element can be of a different data type and a different length</li>
</ul></li>
</ul>
<pre><code class="r">myVec <- c(10, "R", 10-5i, T)
myList <- list(10, "R", 10-5i, T)
myVec
</code></pre>
<pre><code>## [1] "10" "R" "10-5i" "TRUE"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-23" style="background:;">
<hgroup>
<h2>Lists:</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">myList <- list(10, "R", 10-5i, T)
myList
</code></pre>
<pre><code>## [[1]]
## [1] 10
##
## [[2]]
## [1] "R"
##
## [[3]]
## [1] 10-5i
##
## [[4]]
## [1] TRUE
</code></pre>
<ul>
<li>Elements of list are shown with [[]]</li>
<li>Elements of vector are shown with []</li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-24" style="background:;">
<hgroup>
<h2>Data Frames:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>We use data frames to store tabular data</li>
<li>Data frame is a special list where all objects have equal length</li>
<li>The main difference between data.frame and Matrix?</li>
</ul>
<pre><code class="r">studentID <- paste("S#", sample(c(6473:7392), 10), sep = "")
score <- sample(c(0:100), 10)
gender <- sample(c("female", "male"), 10, replace = TRUE)
data <- data.frame(studentID = studentID, score = score, gender = gender)
head(data)
</code></pre>
<pre><code>## studentID score gender
## 1 S#7075 74 female
## 2 S#6834 68 female
## 3 S#7282 10 male
## 4 S#6903 18 female
## 5 S#7067 98 male
## 6 S#7026 7 male
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-25" style="background:;">
<hgroup>
<h2>Subsetting:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>Often times we need to take a subset of a vector, a matrix, a list, or a dataframe.</li>
<li><p>We consider three main operators to take a subset of an object:</p>
<ul>
<li>[ ]: single brackets return an object of the same class of the original object. By using [], we can also choose more than one element.</li>
<li>[[ ]]: double brackets are used primarily for lists and dataframes. </li>
<li>"$": is used primarily for lists and dataframes (similar to double brackets). </li>
</ul></li>
<li><p>With [[ ]] and $, we can only choose one object!</p></li>
<li><p>[[ ]] and $ can return an object with a different class than the original objects we are subsetting from.</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-26" style="background:;">
<hgroup>
<h3>Subsetting examples:</h3>
</hgroup>
<article data-timings="">
<pre><code class="r">myVec <- 10:20
myVec[3]
</code></pre>
<pre><code>## [1] 12
</code></pre>
<pre><code class="r">myList <- list(obj1 = "a", obj2 = 10, obj3 = T, obj4 = 10-5i)
myList[[3]]
</code></pre>
<pre><code>## [1] TRUE
</code></pre>
<pre><code class="r">myList$obj3
</code></pre>
<pre><code>## [1] TRUE
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-27" style="background:;">
<hgroup>
<h2>Subsetting with [ ]:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>By using single bracket, we can choose more than one element of an object.</li>
<li>In this case, index vectors can be very useful:
<ul>
<li>Index vector is a vector of indices of another vector that is used to select a subset of another vector (or Matrix)</li>
</ul></li>
</ul>
<pre><code class="r">x <- seq(from=0, to=100,by=10) # length(x) is ??
IndVec <- c(1, 2, 3, 4, 5) # the first 5 elements
x[IndVec]
</code></pre>
<pre><code>## [1] 0 10 20 30 40
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-28" style="background:;">
<hgroup>
<h2>Index Vectors:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>There are four types of Index vectors:
<ol>
<li>Logical Index Vector: The logical index vector should be of the same length of the vector from which we are selecting a subset. Values corresponding to TRUE in the index vector are selected.</li>
<li>Vector of Positive integers: All the values in this type of index vector must lie in 1:(length(x)).</li>
<li>Vector of Negative integers: This type of index vector indicates the values to be excluded from the
vector.</li>
<li>A Vector of Character Strings: if a vector has a name attribute, we can simply take a subset of the vector by calling the names of the elements.</li>
</ol></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-29" style="background:;">
<hgroup>
<h2>Index Vectors:</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">myVec <- letters[1:10]
names(myVec) <- paste("e", 1:10, sep = "")
myVec
</code></pre>
<pre><code>## e1 e2 e3 e4 e5 e6 e7 e8 e9 e10
## "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
</code></pre>
<pre><code class="r">logIndVec <- rep(c(T, F), each = 5)
logIndVec
</code></pre>
<pre><code>## [1] TRUE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE
</code></pre>
<pre><code class="r">posIndVec <- 1:5
negIndVec <- -6:-10
chIndVec <- c("e1", "e2", "e3", "e4", "e5")
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-30" style="background:;">
<hgroup>
<h2>Index Vectors:</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">myVec[logIndVec]
</code></pre>
<pre><code>## e1 e2 e3 e4 e5
## "a" "b" "c" "d" "e"
</code></pre>
<pre><code class="r">myVec[negIndVec]
</code></pre>
<pre><code>## e1 e2 e3 e4 e5
## "a" "b" "c" "d" "e"
</code></pre>
<pre><code class="r">myVec[chIndVec]
</code></pre>
<pre><code>## e1 e2 e3 e4 e5
## "a" "b" "c" "d" "e"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-31" style="background:;">
<hgroup>
<h2>Logical Index Vectors:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>logical index vectors can be generated by using conditional statements:
<ul>
<li>Using ==, !=, <, >, ...</li>
</ul></li>
</ul>
<pre><code class="r">myVec <- 1:10
logIndVec <- (myVec < 5)
logIndVec
</code></pre>
<pre><code>## [1] TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
</code></pre>
<pre><code class="r">myVec[logIndVec]
</code></pre>
<pre><code>## [1] 1 2 3 4
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-32" style="background:;">
<hgroup>
<h2>Matrix Indexing:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>Similar to vector indexing, we can refer to individual elements of a matrix.</li>
</ul>
<pre><code class="r">myMat <- matrix(1:8, ncol = 4)
myMat
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 3 5 7
## [2,] 2 4 6 8
</code></pre>
<pre><code class="r">myMat[1,1] # refering to an element
</code></pre>
<pre><code>## [1] 1
</code></pre>
<pre><code class="r">myMat[2,] # refering to the second row
</code></pre>
<pre><code>## [1] 2 4 6 8
</code></pre>
<pre><code class="r">myMat[,3] # refering to the third column
</code></pre>
<pre><code>## [1] 5 6
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-33" style="background:;">
<hgroup>
<h2>Matrix Indexing:</h2>
</hgroup>
<article data-timings="">
<ul>
<li>By default, when the retrieved elements of a matrix look like a vector, R drops their dimension attribute. We can turn this feature off by setting drop = FALSE</li>
</ul>
<pre><code class="r">myMat[1,1]
</code></pre>
<pre><code>## [1] 1
</code></pre>
<pre><code class="r">myMat[1,1, drop = FALSE]
</code></pre>
<pre><code>## [,1]
## [1,] 1
</code></pre>
<pre><code class="r">myMat[2,, drop = FALSE]
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 2 4 6 8
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-34" style="background:;">
<hgroup>
<h2>Subsetting Lists:</h2>
</hgroup>
<article data-timings="">
<pre><code class="r">myList <- list(ch = letters[1:2], lg = F, nm = 1:3)
myList
</code></pre>
<pre><code>## $ch
## [1] "a" "b"
##
## $lg
## [1] FALSE
##
## $nm
## [1] 1 2 3
</code></pre>
<pre><code class="r">myList[1] # subset is still a list
</code></pre>
<pre><code>## $ch
## [1] "a" "b"
</code></pre>