-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
27926 lines (27545 loc) · 314 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>R Tutorial</title>
<meta charset="utf-8">
<meta name="description" content="R Tutorial">
<meta name="author" content="Sebastian Campbell and José Padarian">
<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/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. -->
<script data-main="libraries/frameworks/io2012/js/slides"
src="libraries/frameworks/io2012/js/require-1.0.8.min.js">
</script>
<link rel="stylesheet" href = "libraries/widgets/quiz/css/jquery-quiz.css">
<link rel="stylesheet" href = "libraries/widgets/bootstrap/css/bootstrap.css">
<link rel="stylesheet" href = "libraries/widgets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href = "assets/css/ribbons.css">
</head>
<body style="opacity: 0">
<slides class="layout-widescreen">
<!-- LOGO SLIDE -->
<!-- END LOGO SLIDE -->
<!-- TITLE SLIDE -->
<!-- Should I move this to a Local Layout File? -->
<slide class="title-slide segue nobackground">
<aside class="gdbar">
<img src="assets/img/university_sydney_logo.png">
</aside>
<hgroup class="auto-fadein">
<h1>R Tutorial</h1>
<h2>March 2013</h2>
<p>Sebastian Campbell and José Padarian<br/>Use the left and right arrow keys to navigate slides.</p>
</hgroup>
<footer class = 'license'>
<a href='http://creativecommons.org/licenses/by-nc-nd/3.0/'>
<img width = '80px' src = 'http://mirrors.creativecommons.org/presskit/buttons/88x31/png/by-nc-nd.png'>
</a>
</footer>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<hgroup>
<h2>Welcome to R</h2>
</hgroup>
<article>
<p>Hopefully by now you've started up RStudio (or your IDE or text editor of choice) and are greeted by a command prompt:</p>
<p><img src="images/rconsole.png" alt=""></p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>Say Hello!</h2>
</hgroup>
<article>
<p>As is traditional in programming, we'll start off with a <code>Hello world!</code> program. Try the below:</p>
<pre><code class="r">print("Hello world!")
</code></pre>
<pre><code>## [1] "Hello world!"
</code></pre>
<p>Congratulations! You've taken your first steps into R. You've used the function <code>print</code> to print the text "Hello world!" to the screen. Functions are basically like verbs in language, they tell R to do something. Notice the brackets <code>()</code> after the name of the function in which you can tell the function what to do.</p>
<p>Don't worry too much about the <code>[1]</code> at the beginning, we'll explain that soon. Note that you can also type <code>"Hello world!"</code> without the <code>print</code> command and R will assume you meant to print it.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>Some basic maths</h2>
</hgroup>
<article>
<p>We can also use R as a calculator for addition and powers:</p>
<pre><code class="r">2 + 2
</code></pre>
<pre><code>## [1] 4
</code></pre>
<pre><code class="r">2^3
</code></pre>
<pre><code>## [1] 8
</code></pre>
<p>Note that there are other operators we can use, like <code>*</code> for multiplications, <code>-</code> for subtraction and <code>/</code> for division. </p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="quiz" id="slide-4" style="background:;">
<hgroup>
<h2>Quick quiz</h2>
</hgroup>
<article>
<div class="quiz-single well new">
<p>We'll be having a few quizzes like this during this tutorial. No pressure, it's just to check your understanding. Click 'Submit' to check if your answer is right. If you give up 'Check answer' will give you an answer. 'Clear' will allow you to start that quiz from scratch.</p>
<h3>What is the expected result from <code>6 / 2</code>?</h3>
<ol>
<li>1</li>
<li>2</li>
<li><em>3</em></li>
<li>4</li>
</ol>
<button class="quiz-submit btn btn-primary">Submit</button>
<button class="quiz-show-answer btn btn-success">Show Answer</button>
<button class="quiz-clear">Clear</button>
<div class="quiz-hint">
</div>
<div class="quiz-explanation">
<p>It's 3, because $\frac{6}{2} = 3$.</p>
</div>
</div>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-5" style="background:;">
<hgroup>
<h2>Assigning variables</h2>
</hgroup>
<article>
<p>So now we can do some maths, but we don't want to have to remember the results every time. So let's store some as variable, so we can refer to them by name. To assign, we can use either the <code>=</code> or <code><-</code> operator (though <code><-</code> is recommended).</p>
<pre><code class="r">core_volume <- 100
core_mass <- 180
bulk_density <- core_mass/core_volume
bulk_density
</code></pre>
<pre><code>## [1] 1.8
</code></pre>
<p>Note that assignment doesn't print the variable to the console.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-6" style="background:;">
<hgroup>
<h2>Not just numbers</h2>
</hgroup>
<article>
<p>R doesn't just work with numbers, it can also work with "strings" or characters. Strings are denoted by the quotes (<code>""</code>) surrounding them. We've already used these when we made our "Hello world!" program. In fact, we can use the <code>paste</code> function to stick them together. </p>
<pre><code class="r">paste("The bulk density is:", 1.75, sep = " ")
</code></pre>
<pre><code>## [1] "The bulk density is: 1.75"
</code></pre>
<p>The <code>paste</code> function sticks strings and numbers together into a single string. The <code>sep</code> argument says: "Put a space between all of the things I'm sticking together".</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background:;">
<hgroup>
<h2>Making readable results</h2>
</hgroup>
<article>
<p>Let's combine all the things we know so far into something useful. Remember the bulk density we calculated earlier?</p>
<pre><code class="r">paste("The bulk density is:", bulk_density, sep = " ")
</code></pre>
<pre><code>## [1] "The bulk density is: 1.8"
</code></pre>
<p>Here R evaluated our variable <code>bulk_density</code> and added it to the string we were trying to make.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="quiz" id="slide-8" style="background:;">
<hgroup>
<h2>Quiz 2</h2>
</hgroup>
<article>
<div class="quiz-single well new">
<h3>Which of these lines of code will produce the result: <code>"1:2:3:x:y:z"</code></h3>
<ol>
<li><em>paste(1, 2, 3, "x", "y", "z", sep=":")</em></li>
<li>paste("123xyz", sep=":")</li>
<li>paste(1, 2, 3, x, y, z, sep=":")</li>
<li>paste(1, 2, 3, "x", "y", "z")</li>
</ol>
<button class="quiz-submit btn btn-primary">Submit</button>
<button class="quiz-show-answer btn btn-success">Show Answer</button>
<button class="quiz-clear">Clear</button>
<div class="quiz-hint">
</div>
<div class="quiz-explanation">
<p>The first option. The second option won't work as there's only one string, so R can't place separators between strings. The third option is trying to call variables named <code>x</code>, <code>y</code> and <code>z</code> (but it's likely none exist). Finally, the fourth option is space-separated, not colon-separated.</p>
</div>
</div>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h2>Bigger data</h2>
</hgroup>
<article>
<p>So we've been dealing with individual strings and numbers, let's move onto combining those into vectors. A vector is basically just a 1-dimensional array of data. Like a set of numbers or a shopping list. It's really easy to make vectors of sequential integers:</p>
<pre><code class="r">1:10
</code></pre>
<pre><code>## [1] 1 2 3 4 5 6 7 8 9 10
</code></pre>
<pre><code class="r">5:-5
</code></pre>
<pre><code>## [1] 5 4 3 2 1 0 -1 -2 -3 -4 -5
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-10" style="background:;">
<hgroup>
<h2>More vectors</h2>
</hgroup>
<article>
<p>You can also make vectors of strings or numbers using the combine (<code>c</code>) function:</p>
<pre><code class="r">c(5, 4, 3, 2, 8)
</code></pre>
<pre><code>## [1] 5 4 3 2 8
</code></pre>
<pre><code class="r">c("sand", "silt", "clay")
</code></pre>
<pre><code>## [1] "sand" "silt" "clay"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-11" style="background:;">
<hgroup>
<h2>Limits of atomic vectors</h2>
</hgroup>
<article>
<p>But be careful. These kinds of vectors can only take one type of data. If you combine numbers and strings together:</p>
<pre><code class="r">c("SOILID001", 3, 5, "SOILID006", 7)
</code></pre>
<pre><code>## [1] "SOILID001" "3" "5" "SOILID006" "7"
</code></pre>
<p>Then they all become characters.</p>
<p>If you think about it, you can treat numbers as letters, but it's a lot harder to treat letters as numbers.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background:;">
<hgroup>
<h2>Even bigger data</h2>
</hgroup>
<article>
<p>Now that we can make vectors we can make subsets of bigger datasets, matrices and dataframes. Just to start off, we'll use a simple dataset which comes with R: <code>trees</code> (Try <code>?trees</code> for more information about the data). It is a data frame, a collection of vectors as columns. The vectors are allowed to be different types (character, integer, numeric), but they must all be the same length.</p>
<pre><code class="r">trees
</code></pre>
<pre><code>## Girth Height Volume
## 1 8.3 70 10.3
## 2 8.6 65 10.3
## 3 8.8 63 10.2
## 4 10.5 72 16.4
## 5 10.7 81 18.8
## 6 10.8 83 19.7
## 7 11.0 66 15.6
## 8 11.0 75 18.2
## 9 11.1 80 22.6
## 10 11.2 75 19.9
## 11 11.3 79 24.2
## 12 11.4 76 21.0
## 13 11.4 76 21.4
## 14 11.7 69 21.3
## 15 12.0 75 19.1
## 16 12.9 74 22.2
## 17 12.9 85 33.8
## 18 13.3 86 27.4
## 19 13.7 71 25.7
## 20 13.8 64 24.9
## 21 14.0 78 34.5
## 22 14.2 80 31.7
## 23 14.5 74 36.3
## 24 16.0 72 38.3
## 25 16.3 77 42.6
## 26 17.3 81 55.4
## 27 17.5 82 55.7
## 28 17.9 80 58.3
## 29 18.0 80 51.5
## 30 18.0 80 51.0
## 31 20.6 87 77.0
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background:;">
<hgroup>
<h2>Subsetting</h2>
</hgroup>
<article>
<p>The basic form of a subset is <code>object[whichrows, whichcolumns]</code>. Leaving an element blank means select all. So for example, to get the third row of <code>trees</code>, or the second column:</p>
<pre><code class="r">trees[3, ]
</code></pre>
<pre><code>## Girth Height Volume
## 3 8.8 63 10.2
</code></pre>
<pre><code class="r">trees[, 2]
</code></pre>
<pre><code>## [1] 70 65 63 72 81 83 66 75 80 75 79 76 76 69 75 74 85 86 71 64 78 80 74
## [24] 72 77 81 82 80 80 80 87
</code></pre>
<p>You might have noticed the <code>[24]</code> in the second line of <code>trees[, 2]</code>. This indicates that the 72 next to it is the 24th element of that vector. There just isn't enough room on the screen to show it all in one line.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background:;">
<hgroup>
<h2>Finer subsetting</h2>
</hgroup>
<article>
<p>We can get individual values by setting both rows and columns</p>
<pre><code class="r">trees[3, 2]
</code></pre>
<pre><code>## [1] 63
</code></pre>
<pre><code class="r">trees[1, 1]
</code></pre>
<pre><code>## [1] 8.3
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15" style="background:;">
<hgroup>
<h2>The last elements</h2>
</hgroup>
<article>
<p>And we can also use <code>nrow</code> and <code>ncol</code> (gets the number of rows or columns) to get the last elements:</p>
<pre><code class="r">nrow(trees)
</code></pre>
<pre><code>## [1] 31
</code></pre>
<pre><code class="r">trees[nrow(trees), ]
</code></pre>
<pre><code>## Girth Height Volume
## 31 20.6 87 77
</code></pre>
<pre><code class="r">trees[, ncol(trees)]
</code></pre>
<pre><code>## [1] 10.3 10.3 10.2 16.4 18.8 19.7 15.6 18.2 22.6 19.9 24.2 21.0 21.4 21.3
## [15] 19.1 22.2 33.8 27.4 25.7 24.9 34.5 31.7 36.3 38.3 42.6 55.4 55.7 58.3
## [29] 51.5 51.0 77.0
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-16" style="background:;">
<hgroup>
<h2>Why stop at one?</h2>
</hgroup>
<article>
<p>You can also subset collections of elements. To get the fourth to seventh row of the first and third column:</p>
<pre><code class="r">trees[4:7, c(1, 3)]
</code></pre>
<pre><code>## Girth Volume
## 4 10.5 16.4
## 5 10.7 18.8
## 6 10.8 19.7
## 7 11.0 15.6
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="quiz" id="slide-17" style="background:;">
<hgroup>
<h2>Subsetting quiz</h2>
</hgroup>
<article>
<div class="quiz-single well new">
<h3>What is the number in the third last row of the 2nd column of <code>trees</code>?</h3>
<ol>
<li><em>80</em></li>
<li>87</li>
<li>65</li>
<li>10.3</li>
</ol>
<button class="quiz-submit btn btn-primary">Submit</button>
<button class="quiz-show-answer btn btn-success">Show Answer</button>
<button class="quiz-clear">Clear</button>
<div class="quiz-hint">
</div>
<div class="quiz-explanation">
<p>Using the line <code>trees[nrow(trees) - 2, 2]</code> will get you the third last row of the second column. Third last is the last one minus 2 places.</p>
</div>
</div>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background:;">
<hgroup>
<h2>Other ways of extracting columns</h2>
</hgroup>
<article>
<p>Data frames are collections of vectors, so you can use their names to extract them. You can use your original notation and also <code>$</code> notation (which is convenient, but not as powerful).</p>
<pre><code class="r">trees[, "Height"]
</code></pre>
<pre><code>## [1] 70 65 63 72 81 83 66 75 80 75 79 76 76 69 75 74 85 86 71 64 78 80 74
## [24] 72 77 81 82 80 80 80 87
</code></pre>
<pre><code class="r">trees$Height
</code></pre>
<pre><code>## [1] 70 65 63 72 81 83 66 75 80 75 79 76 76 69 75 74 85 86 71 64 78 80 74
## [24] 72 77 81 82 80 80 80 87
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-19" style="background:;">
<hgroup>
<h2>Subsetting by a conditional</h2>
</hgroup>
<article>
<p>Often, we want all the rows that meet a certain condition. Say I want all the trees where the height is greater than 82 ft. First, I can produce a TRUE/FALSE variable, then take only the cases where it is true:</p>
<pre><code class="r">greater82 <- trees$Height > 82
greater82[1:5]
</code></pre>
<pre><code>## [1] FALSE FALSE FALSE FALSE FALSE
</code></pre>
<pre><code class="r">trees[greater82, ]
</code></pre>
<pre><code>## Girth Height Volume
## 6 10.8 83 19.7
## 17 12.9 85 33.8
## 18 13.3 86 27.4
## 31 20.6 87 77.0
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="quiz" id="slide-20" style="background:;">
<hgroup>
<h2>Logical subsetting quiz</h2>
</hgroup>
<article>
<div class="quiz-single well new">
<h3>How many trees are there with a girth less than 12.5 inches?</h3>
<ol>
<li>3</li>
<li>8</li>
<li>0</li>
<li><em>15</em></li>
</ol>
<button class="quiz-submit btn btn-primary">Submit</button>
<button class="quiz-show-answer btn btn-success">Show Answer</button>
<button class="quiz-clear">Clear</button>
<div class="quiz-hint">
</div>
<div class="quiz-explanation">
<p>Using the code <code>trees[trees$Girth < 12.5, ]</code>, we can see that there are 15 trees with a girth less than 12.5. Press 'p' to see the whole expected output.</p>
</div>
</div>
</article>
<!-- Presenter Notes -->
<aside class="note">
<section>
<pre><code class="r">trees[trees$Girth < 12.5, ]
</code></pre>
<pre><code>## Girth Height Volume
## 1 8.3 70 10.3
## 2 8.6 65 10.3
## 3 8.8 63 10.2
## 4 10.5 72 16.4
## 5 10.7 81 18.8
## 6 10.8 83 19.7
## 7 11.0 66 15.6
## 8 11.0 75 18.2
## 9 11.1 80 22.6
## 10 11.2 75 19.9
## 11 11.3 79 24.2
## 12 11.4 76 21.0
## 13 11.4 76 21.4
## 14 11.7 69 21.3
## 15 12.0 75 19.1
</code></pre>
</section>
</aside>
</slide>
<slide class="" id="slide-21" style="background:;">
<hgroup>
<h2>Getting help</h2>
</hgroup>
<article>
<p>But how do I find out about all these functions? There are a few ways of getting help in R:</p>
<ul>
<li>The default help functions. Try <code>?print</code> or <code>help(print)</code></li>
<li>Other help functions: <code>RSiteSearch("soil")</code></li>
<li>StackOverflow</li>
<li>The wonderful Google</li>
</ul>
<p>At the moment, the help files will be fairly hard to get through, but they make more and more sense the more you use R. Some help files will be much simpler than others. If you find the help file impossible to get through, try testing the examples at the bottom of most help files or access them using the <code>examples</code> function.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="quiz" id="slide-22" style="background:;">
<hgroup>
<h2>Getting help quiz</h2>
</hgroup>
<article>
<div class="quiz-single well new">
<h3>What does the <code>sep</code> argument of <code>read.csv</code> do?</h3>
<ol>
<li>Separates the arguments with commas and puts them together</li>
<li><em>Determines the field separator character of a file which has been read in</em></li>
<li>Decides whether to use the Sepine Angulator algorithm</li>
<li>If TRUE, splits files into list elements</li>
</ol>
<button class="quiz-submit btn btn-primary">Submit</button>
<button class="quiz-show-answer btn btn-success">Show Answer</button>
<button class="quiz-clear">Clear</button>
<div class="quiz-hint">
</div>
<div class="quiz-explanation">
<p>The <code>read.csv</code> function entry found using <code>?read.csv</code> or <code>help(read.csv)</code> says <code>sep</code> is the 'field separator character'.</p>
</div>
</div>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-23" style="background:;">
<hgroup>
<h2>Dataframes</h2>
</hgroup>
<article>
<p>Now we're ready to deal with some real data. We have some soil temperature data (<a href="http://ine.uaf.edu/werc/projects/foothills/stations/dfm1/historical.html">University of Alaska Fairbanks, 2011</a>). The best function to read in CSVs is <code>read.csv</code>. Let's store that object and call it <code>soiltempdata</code>. </p>
<pre><code class="r">url <- "https://raw.github.com/sebastian-c/r-mar2013/gh-pages/soiltempdata.csv"
soiltempdata <- read.csv(url)
</code></pre>
<p>That looks like it went through nicely. No errors, no complaints.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-24" style="background:;">
<hgroup>
<h2>Checking your data</h2>
</hgroup>
<article>
<p>It's a good idea to look at the first few rows of your data, to make sure it went through ok. We'll use <code>head</code> to get the first 4 rows of ours.</p>
<pre><code class="r">head(soiltempdata, 4)
</code></pre>
<pre><code>## COL. DATA.TYPE X X.1 X.2 X.3 X.4 X.5 X.6
## 1 1 Date Time (AST)
## 2 2 0-cm Depth Soil Temperature (degrees C)
## 3 3 5-cm Depth Soil Temperature (degrees C)
## 4 4 10-cm Depth Soil Temperature (degrees C)
## X.7 X.8 X.9 X.10 X.11 X.12 X.13 X.14
## 1
## 2
## 3
## 4
</code></pre>
<p>Blech. That's not what we wanted at all. Most of those columns are empty.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-25" style="background:;">
<hgroup>
<h2>Fixing the data</h2>
</hgroup>
<article>
<p>What went wrong? Let's download the data and open it up in Excel or Notepad and look at it (ideally, you should do this before trying to read it in). Click <a href="https://raw.github.com/sebastian-c/r-mar2013/gh-pages/soiltempdata.csv">here</a> to link to the data (if you're feeling adventurous, try using the <code>download.file</code> function to download it. Be sure to use the <code>mode="wb"</code> option). Save it to your hard drive (put it into My Documents). Then open the CSV file.</p>
<p><img src="images/headsoiltempdata.png" height="300px"></p>
<p>Well there's the problem, there's all this other stuff before we get to the actual data.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-26" style="background:;">
<hgroup>
<h2>Getting to work</h2>
</hgroup>
<article>
<p>Set your working directory to where you stored the data. You can use the <code>getwd</code> function to work out what directory you're in and the <code>setwd</code> function to set the working directory. Remember to use forward slashes (<code>/</code>) as the path separator.</p>
<pre><code class="r">getwd()
</code></pre>
<pre><code>## [1] "C:/Research/"
</code></pre>
<pre><code class="r">setwd("C:/Users/Sebastian/Documents")
getwd()
</code></pre>
<pre><code>## [1] "C:/Users/Sebastian/Documents"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="quiz" id="slide-27" style="background:;">
<hgroup>
<h2>Skipping the first few lines</h2>
</hgroup>
<article>
<div class="quiz-single well new">
<p>Ok, so we had a problem with the data before, let's see if we can read it in properly this time. So the first problem is that we have a few lines before we get to the data. We could remove these manually, but R has a neat trick to sort this problem out, the <code>skip</code> argument in <code>read.csv</code>. </p>
<h3>How many lines (rows) do we have to skip?</h3>
<ol>
<li>1</li>
<li>13</li>
<li><em>16</em></li>
<li>17</li>
</ol>
<button class="quiz-submit btn btn-primary">Submit</button>
<button class="quiz-show-answer btn btn-success">Show Answer</button>
<button class="quiz-clear">Clear</button>
<div class="quiz-hint">
</div>
<div class="quiz-explanation">
<p>There are <strong>16</strong> lines we have to skip (not 17, we want to keep the headers).</p>
</div>
</div>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-28" style="background:;">
<hgroup>
<h2>Reading in the data</h2>
</hgroup>
<article>
<p>Now after all these problems, we can finally read in the data and store it as an object. We can see it actually has columns we want.</p>
<pre><code class="r">soiltempdata <- read.csv("soiltempdata.csv", skip = 16)
head(soiltempdata, 3)
</code></pre>
<pre><code>## ID Year Month Day Hour Depth.0.cm Depth.5.cm Depth.10.cm Depth.15.cm
## 1 1 2011 1 1 0 -6.752 -4.940 -5.061 -4.401
## 2 2 2011 1 1 1 -6.727 -4.916 -5.098 -4.378
## 3 3 2011 1 1 2 -6.726 -4.916 -5.098 -4.377
## Depth.20.cm Depth.40.cm Depth.60.cm Depth.80.cm Depth.100.cm
## 1 -3.698 -2.014 0.041 -0.215 7777
## 2 -3.733 -1.993 0.010 -0.195 7777
## 3 -3.733 -1.992 0.010 -0.195 7777
## Depth.120.cm Depth.135.cm Depth.150.cm
## 1 -0.525 -0.577 -0.995
## 2 -0.505 -0.608 -1.028
## 3 -0.504 -0.608 -1.027
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-29" style="background:;">
<hgroup>
<h2>Structure of objects</h2>
</hgroup>
<article>
<p>Another way of examining objects is the <code>str</code> function.</p>
<pre><code class="r">str(soiltempdata)
</code></pre>
<pre><code>## 'data.frame': 6552 obs. of 17 variables:
## $ ID : int 1 2 3 4 5 6 7 8 9 10 ...
## $ Year : int 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 ...
## $ Month : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Day : int 1 1 1 1 1 1 1 1 1 1 ...
## $ Hour : int 0 1 2 3 4 5 6 7 8 9 ...
## $ Depth.0.cm : num -6.75 -6.73 -6.73 -6.65 -6.67 ...
## $ Depth.5.cm : num -4.94 -4.92 -4.92 -4.91 -4.86 ...
## $ Depth.10.cm : num -5.06 -5.1 -5.1 -5.03 -5.04 ...
## $ Depth.15.cm : num -4.4 -4.38 -4.38 -4.37 -4.38 ...
## $ Depth.20.cm : num -3.7 -3.73 -3.73 -3.72 -3.68 ...
## $ Depth.40.cm : num -2.01 -1.99 -1.99 -2.04 -2.05 ...
## $ Depth.60.cm : num 0.041 0.01 0.01 0.02 0.004 0.04 0.078 0.071 0.055 0.05 ...
## $ Depth.80.cm : num -0.215 -0.195 -0.195 -0.236 -0.2 -0.165 -0.177 -0.184 -0.201 -0.205 ...
## $ Depth.100.cm: int 7777 7777 7777 7777 7777 7777 7777 7777 7777 7777 ...
## $ Depth.120.cm: num -0.525 -0.505 -0.504 -0.546 -0.51 -0.526 -0.487 -0.494 -0.511 -0.515 ...
## $ Depth.135.cm: num -0.577 -0.608 -0.608 -0.598 -0.614 -0.578 -0.591 -0.598 -0.615 -0.619 ...
## $ Depth.150.cm: num -0.995 -1.028 -1.027 -1.017 -0.98 ...
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-30" style="background:;">
<hgroup>
<h2>Yet another problem</h2>
</hgroup>
<article>
<p>Something still isn't right. Look at the 100cm depth soil temperature from the previous slide. Note that you can use the <code>$</code> symbol to subset out individual columns.</p>
<pre><code class="r">str(soiltempdata$Depth.100.cm)
</code></pre>
<pre><code>## int [1:6552] 7777 7777 7777 7777 7777 7777 7777 7777 7777 7777 ...
</code></pre>
<p>There is no way that there is a soil with a temperature of 7777 degrees celsius. So why is that present? Well, if we reopen the data in Excel or Notepad, we'll see there were two important lines: </p>
<ul>
<li>NOTE:,6999 DENOTES MISSING DATA OR INVALID DATA</li>
<li>,7777 DENOTES UNCOLLECTED DATA </li>
</ul>
<p>So it would seem that anything with these values should be a missing value. Turns out R has a trick for that too. Let's reread in the data again (last time, I promise).</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-31" style="background:;">
<hgroup>
<h2>na.strings</h2>
</hgroup>
<article>
<p>If we look at the help file for <code>read.csv</code> (<code>?read.csv</code>), we see there's an argument called <code>na.strings</code>. We have two things we need to be NA, so we combine them like we did a few slides ago: <code>c(6999, 7777)</code>.</p>
<pre><code class="r">soiltempdata <- read.csv("soiltempdata.csv", skip = 16, na.strings = c(6999,
7777))
str(soiltempdata$Depth.100.cm)
</code></pre>
<pre><code>## logi [1:6552] NA NA NA NA NA NA ...
</code></pre>
<p>And now it's full of NAs (missing values), as it should be.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-32" style="background:;">
<hgroup>
<h2>Summaries</h2>
</hgroup>
<article>
<p>A summary of a dataframe's columns can easily be obtained with <code>summary</code>:</p>
<pre><code class="r">summary(soiltempdata)
</code></pre>
<pre><code>## ID Year Month Day
## Min. : 1 Min. :2011 Min. :1.00 Min. : 1.0
## 1st Qu.:1639 1st Qu.:2011 1st Qu.:3.00 1st Qu.: 8.0
## Median :3276 Median :2011 Median :5.00 Median :16.0
## Mean :3276 Mean :2011 Mean :5.02 Mean :15.7
## 3rd Qu.:4914 3rd Qu.:2011 3rd Qu.:7.00 3rd Qu.:23.0
## Max. :6552 Max. :2011 Max. :9.00 Max. :31.0
## Hour Depth.0.cm Depth.5.cm Depth.10.cm
## Min. : 0.00 Min. :-10.87 Min. :-9.90 Min. :-9.88
## 1st Qu.: 5.75 1st Qu.: -8.70 1st Qu.:-8.24 1st Qu.:-8.32
## Median :11.50 Median : -3.79 Median :-2.79 Median :-2.92
## Mean :11.50 Mean : 0.44 Mean : 0.17 Mean :-1.07
## 3rd Qu.:17.25 3rd Qu.: 7.33 3rd Qu.: 6.32 3rd Qu.: 4.49
## Max. :23.00 Max. : 37.21 Max. :32.20 Max. :22.02
## Depth.15.cm Depth.20.cm Depth.40.cm Depth.60.cm
## Min. :-9.52 Min. :-9.33 Min. :-8.81 Min. :-7.787
## 1st Qu.:-8.04 1st Qu.:-7.86 1st Qu.:-7.51 1st Qu.:-6.435
## Median :-2.58 Median :-2.28 Median :-1.82 Median :-1.444
## Mean :-1.51 Mean :-1.87 Mean :-2.75 Mean :-2.846
## 3rd Qu.: 3.90 3rd Qu.: 3.31 3rd Qu.: 1.08 3rd Qu.: 0.199
## Max. :14.76 Max. :10.35 Max. : 4.03 Max. : 1.251
## Depth.80.cm Depth.100.cm Depth.120.cm Depth.135.cm
## Min. :-7.473 Mode:logical Min. :-6.935 Min. :-6.709
## 1st Qu.:-6.212 NA's:6552 1st Qu.:-5.713 1st Qu.:-5.495
## Median :-2.009 Median :-2.482 Median :-2.577
## Mean :-3.170 Mean :-3.230 Mean :-3.205
## 3rd Qu.:-0.449 3rd Qu.:-0.977 3rd Qu.:-1.096
## Max. :-0.122 Max. :-0.487 Max. :-0.574
## Depth.150.cm
## Min. :-6.90
## 1st Qu.:-5.69
## Median :-3.04
## Mean :-3.59
## 3rd Qu.:-1.63
## Max. :-0.98
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-33" style="background:;">
<hgroup>
<h2>Histogram</h2>
</hgroup>
<article>
<p>A simple exploratory analysis could start, for example, with a histogram of the data to observe the distribution of the data. Try using the function <code>hist</code> with the temperatures at depth 0:</p>
<pre><code class="r">hist(soiltempdata$Depth.0.cm)
</code></pre>
<p><img src="figure/unnamed-chunk-27.png" alt="plot of chunk unnamed-chunk-27"> </p>
<p>(Pretty skewed, huh?)</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-34" style="background:;">
<hgroup>
<h2>Plotting</h2>
</hgroup>
<article>
<p>We can now do some basic plotting. Let's look at the 0cm depth over time. This is easy using the <code>plot</code> function. We need specify the x variable and the y variable. We'll use the <code>type="l"</code> argument to make it a line plot (the default has points):</p>
<pre><code class="r">plot(soiltempdata$ID, soiltempdata$Depth.0.cm, type = "l")
</code></pre>
<p><img src="figure/unnamed-chunk-28.png" alt="plot of chunk unnamed-chunk-28"> </p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-35" style="background:;">
<hgroup>
<h2>Plotting a subset</h2>
</hgroup>
<article>