-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1425 lines (1110 loc) · 42.1 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="en">
<head>
<meta charset="utf-8">
<title>Intro to R</title>
<meta name="description" content="">
<meta name="author" content="Ankit Sharma">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="libraries/frameworks/revealjs/css/reveal.min.css">
<link rel="stylesheet" href="libraries/frameworks/revealjs/css/theme/solarized.css" id="theme">
<link rel="stylesheet" href="libraries/highlighters/highlight.js/css/zenburn.css" id="theme">
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]--> <link rel="stylesheet" href = "assets/css/ribbons.css">
</head>
<body>
<div class="reveal">
<div class="slides">
<section class='' data-state='' id='slide-1'>
<h1>Intro to R</h1>
<h3>A brief introduction to R statistical environment</h3>
<p><small> Created by <a href="http://in.linkedin.com/in/aks11588/">Ankit Sharma</a> / <a href="http://diggdata.in">Digg Data</a> </small></p>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<aside class='notes'>
<p>Some notes on the first slide</p>
</aside>
</section>
<section>
<section class='' data-state=''>
<h3>Basic operations</h3>
<p>Addition, Subtraction, Multiplication, Division, Remainder</p>
<pre><code class="r">1 + 3
</code></pre>
<pre><code>## [1] 4
</code></pre>
<pre><code class="r">4 * 2 - 5
</code></pre>
<pre><code>## [1] 3
</code></pre>
<pre><code class="r">42/23 + 43
</code></pre>
<pre><code>## [1] 44.83
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>Creating a vector</p>
<pre><code class="r">c(3, 2, 5, 1, 3, 5, 8, 9, 6, 3, 2)
</code></pre>
<pre><code>## [1] 3 2 5 1 3 5 8 9 6 3 2
</code></pre>
<pre><code class="r">3:45
</code></pre>
<pre><code>## [1] 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
## [24] 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>Arithmatic operations on vectors</p>
<pre><code class="r">c(1, 2, 3, 4) * c(10, 20, 30, 40)
</code></pre>
<pre><code>## [1] 10 40 90 160
</code></pre>
<p>if two vectors are not of equal lengths </p>
<pre><code class="r">c(1, 2, 3, 4, 5) * c(10, 20, 30, 40)
</code></pre>
<pre><code>## Warning: longer object length is not a multiple of shorter object length
</code></pre>
<pre><code>## [1] 10 40 90 160 50
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>You can create a character vector as well</p>
<p><small><em>Note: You can using assignment operator '<-' to assign this vector to a variable name "mydata"</em></small></p>
<pre><code class="r">mydata <- c("Hello world", "Data Science Practices", "Bangalore")
print(mydata)
</code></pre>
<pre><code>## [1] "Hello world" "Data Science Practices"
## [3] "Bangalore"
</code></pre>
<pre><code class="r">mydata
</code></pre>
<pre><code>## [1] "Hello world" "Data Science Practices"
## [3] "Bangalore"
</code></pre>
<aside class='notes'>
</aside>
</section>
</section>
<section class='' data-state='' id='slide-3'>
<h2>Navigation within R environment</h2>
<ul>
<li>Listing all files</li>
</ul>
<pre><code class="r">ls()
</code></pre>
<ul>
<li>Examining a variable</li>
</ul>
<pre><code class="r">str() # Compactly display the internal structure of an R object
summary() # Displays summary of the data like mean, median, 1st and 3rd quantile, number of NA(missing values)
head() # Displays first 6 rows of the data
tail() # Displays last 6 rows of the data
class() # Displays class of object - character, integer, numeric, factor, logical
describe() # part of HMisc package, basically a mixture of str() and summary()
</code></pre>
<ul>
<li>Removing variables from environment</li>
</ul>
<pre><code class="r">rm()
rm(list = ls()) # Removes everything
</code></pre>
</section>
<section>
<section class='' data-state=''>
<h2>Data Structures in R</h2>
<ol>
<li><p>Vectors:</p>
<ul>
<li>Numeric</li>
<li>Character</li>
<li>Logical</li>
<li>Factors</li>
</ul></li>
<li><p>Objects:</p>
<ul>
<li>Arrays & Matrices</li>
<li>Lists</li>
<li>Data Frames</li>
</ul></li>
</ol>
<blockquote>
<p><strong>Data type conversion</strong></p>
</blockquote>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Vectors</h2>
<pre><code class="r">x <- c(0, 2:4)
class(x)
</code></pre>
<pre><code>## [1] "numeric"
</code></pre>
<pre><code class="r">y <- c("alpha", "beta", "gamma", "32", "r2")
class(y)
</code></pre>
<pre><code>## [1] "character"
</code></pre>
<pre><code class="r">z <- as.logical(c(1, 0, TRUE, F, 1, T, FALSE))
class(z)
</code></pre>
<pre><code>## [1] "logical"
</code></pre>
<pre><code class="r">f <- as.ordered(factor(c("Good", "Normal", "Bad", "Good", "Bad", "Good", "Normal"),
levels = c("Bad", "Normal", "Good")))
class(f)
</code></pre>
<pre><code>## [1] "ordered" "factor"
</code></pre>
<pre><code class="r">str(f)
</code></pre>
<pre><code>## Ord.factor w/ 3 levels "Bad"<"Normal"<..: 3 2 1 3 1 3 2
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<ul>
<li>Arrays</li>
</ul>
<pre><code class="r">myarray <- array(c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), dim = c(3, 4))
print(myarray)
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
</code></pre>
<p>referencing a call</p>
<pre><code class="r">myarray[2, 3]
</code></pre>
<pre><code>## [1] 8
</code></pre>
<p><small> To get all rows (or columns) from a dimension, simply omit the indices. The following code is for showing all columns of 2nd row </small></p>
<pre><code class="r">myarray[2, ]
</code></pre>
<pre><code>## [1] 2 5 8 11
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<ul>
<li>Matrices</li>
</ul>
<p><small> Well, matrix is nothing but a 2-dimensional array </small></p>
<pre><code class="r">matrix(data = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12), nrow = 3, ncol = 4)
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12
</code></pre>
<pre><code class="r">data <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
matrix(data, ncol = 3, byrow = F) # Filling Column-wise
</code></pre>
<pre><code>## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
</code></pre>
<pre><code class="r">matrix(data, ncol = 3, byrow = T) # Filling Row-wise
</code></pre>
<pre><code>## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<ul>
<li>Lists</li>
</ul>
<p><small>In R, it is possible to construct more complicated structures with multiple data types. R has a built-in data type for mixing objects of different types, called <strong>lists</strong>. </small></p>
<pre><code class="r">ndata = c(2, 3, 5)
sdata = c("aa", "bb", "cc", "dd", "ee")
ldata = c(TRUE, FALSE, TRUE, FALSE, FALSE)
mydata = list(ndata, sdata, ldata, 3) # x contains copies of n, s, b
</code></pre>
<p>Referencing list</p>
<pre><code class="r">mydata[2]
</code></pre>
<pre><code>## [[1]]
## [1] "aa" "bb" "cc" "dd" "ee"
</code></pre>
<pre><code class="r">mydata[c(1, 3)]
</code></pre>
<pre><code>## [[1]]
## [1] 2 3 5
##
## [[2]]
## [1] TRUE FALSE TRUE FALSE FALSE
</code></pre>
<pre><code class="r">mydata[[1]][3] # Member referencing
</code></pre>
<pre><code>## [1] 5
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<ul>
<li>Data Frames</li>
</ul>
<p><small> A data frame is used for storing data tables. It is a list of vectors of equal length. For example, the following variable df is a data frame containing three vectors a, b, c.</small> </p>
<pre><code class="r">a <- c(2, 3, 5)
b <- c("aa", "bb", "cc")
c <- c(TRUE, FALSE, TRUE)
df <- data.frame(a, b, c)
df
</code></pre>
<pre><code>## a b c
## 1 2 aa TRUE
## 2 3 bb FALSE
## 3 5 cc TRUE
</code></pre>
<p><small>Built-in data frame like <strong>iris</strong>, <strong>mcars</strong>, <strong>Insurance</strong>, etc.</small></p>
<pre><code class="r">class(iris)
</code></pre>
<pre><code>## [1] "data.frame"
</code></pre>
<pre><code class="r">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>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Data type conversion</h2>
<p>Use is.foo to test for data type foo. Returns TRUE/FALSE<br/>
Use as.foo to explicitly convert it.</p>
<ul>
<li>is.numeric(), is.character(), is.vector(), is.matrix(), is.data.frame()</li>
<li>as.numeric(), as.character(), as.vector(), as.matrix(), as.data.frame)</li>
</ul>
<p><img src="images/DataConversion.png"></p>
<aside class='notes'>
</aside>
</section>
</section>
<section>
<section class='' data-state=''>
<h2>Working with Data Frames</h2>
<ul>
<li>Manipulation</li>
<li>Indexing</li>
<li>Transform data</li>
</ul>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Manipulation</h2>
<p>Delecting a column</p>
<pre><code class="r">data(iris)
dim(iris)
</code></pre>
<pre><code>## [1] 150 5
</code></pre>
<pre><code class="r">iris$Sepal.Width <- NULL # or try iris[,2] <- NULL
dim(iris)
</code></pre>
<pre><code>## [1] 150 4
</code></pre>
<p>Delecting a row</p>
<pre><code class="r">data(iris)
dim(iris)
</code></pre>
<pre><code>## [1] 150 4
</code></pre>
<pre><code class="r">iris <- iris[-c(5:10), ] # Removing the 5th-10th rows, total 6 rows
dim(iris)
</code></pre>
<pre><code>## [1] 144 4
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Indexing</h2>
<ol>
<li>array of integer indices</li>
<li>array of character names</li>
<li>array of logical booleans</li>
</ol>
<pre><code class="r">mydata <- list(bangalore = c(2, 3, 1, 4, 2), indore = c(7, 2, 4, 9, 8), noida = c(5,
2, 6, 8, 6))
mydata <- as.data.frame(mydata)
mydata[2:4, 1:2] # integer indices
</code></pre>
<pre><code>## bangalore indore
## 2 3 2
## 3 1 4
## 4 4 9
</code></pre>
<pre><code class="r">mydata[, "indore"] # character indices
</code></pre>
<pre><code>## [1] 7 2 4 9 8
</code></pre>
<pre><code class="r">mydata[mydata$bangalore > 2, ] # boolean logic
</code></pre>
<pre><code>## bangalore indore noida
## 2 3 2 2
## 4 4 9 8
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Transform data</h2>
<p>Subset</p>
<pre><code class="r">newdata <- subset(iris, Species == "setosa")
summary(newdata)
</code></pre>
<pre><code>## Sepal.Length Petal.Length Petal.Width Species
## Min. :4.30 Min. :1.00 Min. :0.100 setosa :44
## 1st Qu.:4.80 1st Qu.:1.38 1st Qu.:0.200 versicolor: 0
## Median :5.00 Median :1.50 Median :0.200 virginica : 0
## Mean :5.02 Mean :1.46 Mean :0.248
## 3rd Qu.:5.20 3rd Qu.:1.60 3rd Qu.:0.300
## Max. :5.80 Max. :1.90 Max. :0.600
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Transform data (cond...)</h2>
<p>Transform</p>
<pre><code class="r">newdata <- transform(iris, ratio = Petal.Width/Petal.Length, Sepal.Length = NULL)
summary(newdata)
</code></pre>
<pre><code>## Petal.Length Petal.Width Species ratio
## Min. :1.00 Min. :0.100 setosa :44 Min. :0.0667
## 1st Qu.:1.60 1st Qu.:0.375 versicolor:50 1st Qu.:0.2308
## Median :4.40 Median :1.350 virginica :50 Median :0.3061
## Mean :3.85 Mean :1.240 Mean :0.2872
## 3rd Qu.:5.10 3rd Qu.:1.800 3rd Qu.:0.3586
## Max. :6.90 Max. :2.500 Max. :0.4706
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Transform data (cond...)</h2>
<p>Sample</p>
<pre><code class="r">mysample <- iris[sample(1:nrow(iris), 50, replace = FALSE), ]
summary(mysample)
</code></pre>
<pre><code>## Sepal.Length Petal.Length Petal.Width Species
## Min. :4.40 Min. :1.00 Min. :0.100 setosa :14
## 1st Qu.:5.10 1st Qu.:1.62 1st Qu.:0.375 versicolor:18
## Median :5.95 Median :4.50 Median :1.350 virginica :18
## Mean :5.89 Mean :3.89 Mean :1.240
## 3rd Qu.:6.47 3rd Qu.:5.20 3rd Qu.:1.800
## Max. :7.90 Max. :6.70 Max. :2.300
</code></pre>
<aside class='notes'>
</aside>
</section>
</section>
<section>
<section class='' data-state=''>
<h2>Control structures & Functions</h2>
<p>R has the following control structures:</p>
<ol>
<li>if-else</li>
<li>for</li>
<li>while</li>
<li>switch</li>
<li>ifelse</li>
</ol>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p><strong>Functions</strong></p>
<p>R has lot of <a href="http://www.statmethods.net/management/functions.html">Build-in functions</a> like mean(), tolower(), dnorm(), na.rm(), is.na(), t() for various analysis.<br/>
The general structure for any user-defined function in R is:</p>
<pre><code class="r">myfunction <- function(arg1, arg2, ...) {
statements
return(object)
}
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Example</h2>
<p>Transpose of a matrix</p>
<pre><code class="r">mytrans <- function(x) {
if (!is.matrix(x)) {
warning("argument is not a matrix: returning NA")
return(NA_real_)
}
y <- matrix(1, nrow = ncol(x), ncol = nrow(x))
for (i in 1:nrow(x)) {
for (j in 1:ncol(x)) {
y[j, i] <- x[i, j]
}
}
return(y)
}
# try it
z <- matrix(1:10, nrow = 5, ncol = 2)
tz <- mytrans(z)
tz
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10
</code></pre>
<aside class='notes'>
</aside>
</section>
</section>
<section>
<section class='' data-state=''>
<h2>Working with data files</h2>
<ul>
<li>Getting data into R</li>
<li>Getting data out of R</li>
</ul>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Getting data into R</h2>
<p><small>- From data files (csv, tsv, txt, ...)
<em>Also look at read.delim, read.table functions</em>
</small></p>
<pre><code class="r">mydata <- read.csv("/file path/file_name.csv", header = TRUE)
</code></pre>
<p><small>- From databases
packages require <strong>sqldf</strong>, <strong>RSQLite</strong>, <strong>RSQLite.extfuns</strong>, <strong>gsubfn</strong>, <strong>DBI</strong>, <strong>chron</strong></small></p>
<pre><code class="r">connection <- dbConnect(driver, user, pass, host, dbname)
mydata <- dbSendQuery(connection, "SELECT * FROM table_name")
</code></pre>
<p><small>- From Web </small></p>
<pre><code class="r">connection <- url("http://www.example.com/datafile/data.txt")
mydata <- read.csv(connection, header = TRUE)
</code></pre>
<p><small>- From RData files</small></p>
<pre><code class="r">mydata <- data("Data_File_Name.Rdata")
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Getting data out of R</h2>
<p><small>- To data files (csv, tsv, txt, ...)</small></p>
<pre><code class="r">write.csv("/file path/file_name.csv", header = TRUE, row.names = NULL)
</code></pre>
<p><small>- To databases </small></p>
<pre><code class="r">connection <- dbConnect(driver, user, pass, host, dbname)
dbWriteTable(connection, "table_name", data_name)
</code></pre>
<p><small>- To RData file
</small></p>
<pre><code class="r">save(mydata, file = "mydata.RData")
</code></pre>
<aside class='notes'>
</aside>
</section>
</section>
<section>
<section class='' data-state=''>
<h2>Data Processing</h2>
<ol>
<li>Loading data into R</li>
<li>Analyzing data</li>
<li>Writing it out on a csv and RData file</li>
</ol>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Loading data into R</h2>
<pre><code class="r">data(iris)
dim(iris)
</code></pre>
<pre><code>## [1] 144 4
</code></pre>
<pre><code class="r">head(iris) #You can also do tails(iris) to get last 6 rows
</code></pre>
<pre><code>## Sepal.Length Petal.Length Petal.Width Species
## 1 5.1 1.4 0.2 setosa
## 2 4.9 1.4 0.2 setosa
## 3 4.7 1.3 0.2 setosa
## 4 4.6 1.5 0.2 setosa
## 11 5.4 1.5 0.2 setosa
## 12 4.8 1.6 0.2 setosa
</code></pre>
<pre><code class="r">summary(iris)
</code></pre>
<pre><code>## Sepal.Length Petal.Length Petal.Width Species
## Min. :4.30 Min. :1.00 Min. :0.100 setosa :44
## 1st Qu.:5.17 1st Qu.:1.60 1st Qu.:0.375 versicolor:50
## Median :5.80 Median :4.40 Median :1.350 virginica :50
## Mean :5.88 Mean :3.85 Mean :1.240
## 3rd Qu.:6.40 3rd Qu.:5.10 3rd Qu.:1.800
## Max. :7.90 Max. :6.90 Max. :2.500
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Analyzing data</h2>
<pre><code class="r">dim(iris)
</code></pre>
<pre><code>## [1] 150 5
</code></pre>
<pre><code class="r">head(iris) #You can also do tails(iris) to get last 6 rows
</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa
</code></pre>
<pre><code class="r">summary(iris) #You can also try str(iris) to show the compatibility display of objects
</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.30 Min. :2.00 Min. :1.00 Min. :0.1
## 1st Qu.:5.10 1st Qu.:2.80 1st Qu.:1.60 1st Qu.:0.3
## Median :5.80 Median :3.00 Median :4.35 Median :1.3
## Mean :5.84 Mean :3.06 Mean :3.76 Mean :1.2
## 3rd Qu.:6.40 3rd Qu.:3.30 3rd Qu.:5.10 3rd Qu.:1.8
## Max. :7.90 Max. :4.40 Max. :6.90 Max. :2.5
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Writing it out a csv & RData</h2>
<pre><code class="r">write.csv("myIrisData", row.names = NULL)
save("iris", file = "myIrisRdata.RData")
</code></pre>
<p>Plotting some data</p>
<pre><code class="r">plot(iris$Sepal.Length, xlab = "Index", ylab = "Length")
</code></pre>
<p><img src="assets/fig/unnamed-chunk-36.png" alt="plot of chunk unnamed-chunk-36"> </p>
<aside class='notes'>
</aside>
</section>
</section>
<section>
<section class='' data-state=''>
<h2>Apply functions</h2>
<p><small>The following are the apply functions in the base package in R which should be used instead of loops.</p>
<ol>
<li><strong><em>apply</em></strong> Apply Functions Over Array Margins</li>
<li><strong><em>by</em></strong> Apply a Function to a Data Frame Split by Factors</li>
<li><strong><em>eapply</em></strong> Apply a Function Over Values in an Environment</li>
<li><strong><em>lapply</em></strong> Apply a Function over a List or Vector
<ul>
<li><strong><em>sapply</em></strong></li>
<li><strong><em>vapply</em></strong></li>
</ul></li>
<li><strong><em>mapply</em></strong> Apply a Function to Multiple List or Vector Arguments</li>
<li><strong><em>rapply</em></strong> Recursively Apply a Function to a List</li>
<li><strong><em>tapply</em></strong> Apply a Function Over a Ragged Array</li>
</ol>
<p></small></p>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>lapply</h2>
<pre><code class="r"># create a list with 2 elements
l <- list(a = 1:10, b = 11:20)
head(l)
</code></pre>
<pre><code>## $a
## [1] 1 2 3 4 5 6 7 8 9 10
##
## $b
## [1] 11 12 13 14 15 16 17 18 19 20
</code></pre>
<pre><code class="r"># the mean of the values in each element
lapply(l, mean)
</code></pre>
<pre><code>## $a
## [1] 5.5
##
## $b
## [1] 15.5
</code></pre>
<pre><code class="r"># the sum of the values in each element
lapply(l, sum)
</code></pre>
<pre><code>## $a
## [1] 55
##
## $b
## [1] 155
</code></pre>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Replicate</h2>
<pre><code class="r">replicate(5, rnorm(10)) # rnorm() is random generation for the normal distribution with mean equal to mean and standard deviation equal to sd.
</code></pre>
<pre><code>## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.3107 0.18165 -1.20096 -0.32282 0.6487
## [2,] -0.5871 -0.31135 0.08768 -1.67377 -0.6564
## [3,] 0.8028 0.03560 0.36174 0.05899 0.1640
## [4,] 0.3906 1.06573 0.32263 0.35181 -0.1309
## [5,] -0.9278 -0.06597 -2.36410 1.08502 -1.2519
## [6,] 0.4756 -0.90339 -0.20174 0.07512 -0.2367
## [7,] 1.3457 -0.37886 -1.00612 1.16226 0.6133
## [8,] 0.4713 0.44807 2.65176 -1.65329 0.7477
## [9,] 0.3419 1.82994 0.18421 0.77529 1.7285
## [10,] 2.3811 -0.37091 -0.68428 0.09878 0.2190
</code></pre>
<pre><code class="r">replicate(10, "impetus")
</code></pre>
<pre><code>## [1] "impetus" "impetus" "impetus" "impetus" "impetus" "impetus" "impetus"
## [8] "impetus" "impetus" "impetus"
</code></pre>
<aside class='notes'>
</aside>
</section>
</section>
<section>
<section class='' data-state=''>
<h2>Graphics and Visualization</h2>
<p>There are 3 major ways of creating graphics in R:</p>
<ol>
<li>Base R graphics</li>
<li><a href="http://ggplot2.org/">ggplot2 package</a></li>
<li><a href="http://lmdvr.r-forge.r-project.org/figures/figures.html">lattice package</a></li>
</ol>
<p>Both "lattice" and "ggplot2" are based on grid graphics sub-system.<br/> A highly useful presentation for <a href="http://www.slideshare.net/izahn/rgraphics-12040991">building graphics in ggplot2</a>.</p>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<h2>Basic graphics in R</h2>
<ul>
<li>Density plot</li>
<li>Dot plot</li>
<li>Bar plot</li>
<li>Line plot</li>
<li>Pie plot</li>
<li>Box plot</li>
<li>Scatter plot</li>
</ul>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>Density plot</p>
<pre><code class="r">x <- mtcars$mpg
h <- hist(x, breaks = 10, col = "red", xlab = "Miles Per Gallon", main = "Histogram with Normal Curve")
xfit <- seq(min(x), max(x), length = 40)
yfit <- dnorm(xfit, mean = mean(x), sd = sd(x))
yfit <- yfit * diff(h$mids[1:2]) * length(x)
lines(xfit, yfit, col = "blue", lwd = 2)
</code></pre>
<p><img src="assets/fig/unnamed-chunk-39.png" alt="plot of chunk unnamed-chunk-39"> </p>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>Dot plot</p>
<pre><code class="r">dotchart(mtcars$mpg, labels = row.names(mtcars), cex = 0.7, main = "Gas Milage for Car Models",
xlab = "Miles Per Gallon")
</code></pre>
<p><img src="assets/fig/unnamed-chunk-40.png" alt="plot of chunk unnamed-chunk-40"> </p>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>Bar plot</p>
<pre><code class="r">counts <- table(mtcars$gear)
barplot(counts, main = "Car Distribution", xlab = "Number of Gears")
</code></pre>
<p><img src="assets/fig/unnamed-chunk-41.png" alt="plot of chunk unnamed-chunk-41"> </p>
<aside class='notes'>
</aside>
</section>
<section class='' data-state=''>
<p>Line plot</p>
<pre><code class="r">x <- c(-3:3)
y <- x^2 + 2 # create some data
par(pch = 22, col = "blue") # plotting symbol and color
opts = "c"