forked from AdamWilsonLabEDU/SpatialDataScience
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_Rintro.html
More file actions
741 lines (661 loc) · 22.9 KB
/
01_Rintro.html
File metadata and controls
741 lines (661 loc) · 22.9 KB
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
<!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" />
<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/cosmo.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/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.0.13/css/fa-svg-with-js.css" rel="stylesheet" />
<script src="site_libs/font-awesome-5.0.13/js/fontawesome-all.min.js"></script>
<script src="site_libs/font-awesome-5.0.13/js/fa-v4-shims.min.js"></script>
<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>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<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%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
</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;
}
</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>
<div class="container-fluid main-container">
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_').toLowerCase();
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
padding-left: 25px;
text-indent: 0;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-inverse 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">GEO 503: Spatial 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>
<a href="Syllabus.html">Syllabus</a>
</li>
<li>
<a href="Schedule.html">Schedule</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Content
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="CourseContent.html">About Course Content</a>
</li>
<li class="dropdown-header">Module 1: Introduction to R</li>
<li>
<a href="00_CourseIntroductionFrame.html">00 Course Introduction</a>
</li>
<li>
<a href="01_Rintro.html">01 First Steps</a>
</li>
<li>
<a href="02_graphics.html">02 Graphics</a>
</li>
<li>
<a href="03_DataWrangling.html">03 Data Wrangling</a>
</li>
<li>
<a href="03b_DataWrangling.html">03 Data Wrangling 2</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Module 2: Spatial Analyses</li>
<li>
<a href="04_Spatial_with_sf.html">04 Spatial Data with sf</a>
</li>
<li>
<a href="05_Raster.html">05 Spatial Raster Data</a>
</li>
<li class="divider"></li>
<li class="dropdown-header">Module 3: Advanced Programming</li>
<li>
<a href="06_CreatingWorkflows.html">06 Creating Workflows</a>
</li>
<li>
<a href="07_Reproducibile.html">07 Reproducible Research</a>
</li>
<li>
<a href="08_WeatherData.html">08 Weather Data Processing</a>
</li>
<li>
<a href="09_RemoteSensing_appeears.html">09 Satellite Data Processing</a>
</li>
<li>
<a href="11_ParallelProcessing.html">11 Parallel Processing</a>
</li>
<li>
<a href="12_DynamicVisualization.html">12 Dynamic Visualization</a>
</li>
<li>
<a href="13_SDM_Exercise.html">13 Species Distribution Modeling</a>
</li>
<li class="divider"></li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Assignments
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Tasklist.html">Task list</a>
</li>
<li>
<a href="DataCamp.html">DataCamp</a>
</li>
<li>
<a href="PackageIntro.html">Package Introduction</a>
</li>
<li>
<a href="Project.html">Final Project</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Resources
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="Provenance.html">Provenance</a>
</li>
<li>
<a href="Projects_2017.html">2017 Final Projects</a>
</li>
<li>
<a href="Resources.html">Resources</a>
</li>
<li>
<a href="GitSSHNotes.html">Setting up Github</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/adammwilson/RDataScience">
<span class="fa fa-github 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>
<div id="logistics" class="section level1">
<h1>Logistics</h1>
<p><a href="presentations/PS_01_intro.html" target="_blank"><i class="fas fa-desktop fa-3x" aria-hidden="true"></i> Presentation</a></p>
<p><a href="scripts/01_Rintro.R" target="_blank"><i class="fa fa-file-code-o fa-3x" aria-hidden="true"></i> R Script</a> Download this file and open it (or copy-paste into a new script) with RStudio so you can follow along.</p>
</div>
<div id="first-steps" class="section level1">
<h1>First Steps</h1>
<div id="variables" class="section level2">
<h2>Variables</h2>
<pre class="r"><code>x=1
x</code></pre>
<pre><code>## [1] 1</code></pre>
<p>We can also assign a vector to a variable:</p>
<pre class="r"><code>x=c(5,8,14,91,3,36,14,30)
x</code></pre>
<pre><code>## [1] 5 8 14 91 3 36 14 30</code></pre>
<p>And do simple arithmetic:</p>
<pre class="r"><code>x+2</code></pre>
<pre><code>## [1] 7 10 16 93 5 38 16 32</code></pre>
<div class="well">
<p>Create a new variable called <code>y</code> and set it to <code>15</code></p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo1">
Show Solution
</button>
<div id="demo1" class="collapse">
<pre class="r"><code>y=15</code></pre>
</div>
</div>
<p>Note that <code>R</code> is case sensitive, if you ask for <code>X</code> instead of <code>x</code>, you will get an error</p>
<pre class="r"><code>X
Error: object 'X' not found</code></pre>
<div id="variable-naming-conventions" class="section level3">
<h3>Variable naming conventions</h3>
<p>Naming your variables is your business, but there are <a href="http://www.r-bloggers.com/consistent-naming-conventions-in-r/">5 conventions</a> to be aware of:</p>
<ul>
<li><strong>alllowercase</strong>: <em>e.g.</em> <code>adjustcolor</code></li>
<li><strong>period.separated</strong>: <em>e.g.</em> <code>plot.new</code></li>
<li><strong>underscore_separated</strong>: <em>e.g.</em> <code>numeric_version</code></li>
<li><strong>lowerCamelCase</strong>: <em>e.g.</em> <code>addTaskCallback</code></li>
<li><strong>UpperCamelCase</strong>: <em>e.g.</em> <code>SignatureMethod</code></li>
</ul>
</div>
</div>
</div>
<div id="subsetting" class="section level1">
<h1>Subsetting</h1>
<pre class="r"><code>x</code></pre>
<pre><code>## [1] 5 8 14 91 3 36 14 30</code></pre>
<p>Subset the vector using <code>x[ ]</code> notation</p>
<pre class="r"><code>x[5]</code></pre>
<pre><code>## [1] 3</code></pre>
<p>You can use a <code>:</code> to quickly generate a sequence:</p>
<pre class="r"><code>1:5</code></pre>
<pre><code>## [1] 1 2 3 4 5</code></pre>
<p>and use that to subset as well:</p>
<pre class="r"><code>x[1:5]</code></pre>
<pre><code>## [1] 5 8 14 91 3</code></pre>
</div>
<div id="using-functions" class="section level1">
<h1>Using Functions</h1>
<p>To calculate the mean, you could do it <em>manually</em> like this</p>
<pre class="r"><code>(5+8+14+91+3+36+14+30)/8</code></pre>
<pre><code>## [1] 25.125</code></pre>
<p>Or use a function:</p>
<pre class="r"><code>mean(x)</code></pre>
<pre><code>## [1] 25.125</code></pre>
<p>Type <code>?functionname</code> to get the documentation (<code>?mean</code>) or <code>??"search parameters</code> (??“standard deviation”) to search the documentation. In RStudio, you can also search in the help panel. <code>mean</code> has other arguments too:</p>
<p><code>mean(x, trim = 0, na.rm = FALSE, ...)</code></p>
<p>In RStudio, if you press <code>TAB</code> after a function name (such as <code>mean(</code>), it will show function arguments.</p>
<div class="figure">
<img src="img/autocomplete.png" title="Autocomplete Screenshot" alt="Autocomplete screenshot" />
<p class="caption">Autocomplete screenshot</p>
</div>
<div class="well">
<p>Calculate the standard deviation of <code>c(3,6,12,89)</code>.</p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo2">
Show Solution
</button>
<div id="demo2" class="collapse">
<pre class="r"><code>y=c(3,6,12,89)
sqrt((sum((y-mean(y))^2))/(length(y)-1))</code></pre>
<pre><code>## [1] 41.17038</code></pre>
<pre class="r"><code>#or
sd(y)</code></pre>
<pre><code>## [1] 41.17038</code></pre>
<pre class="r"><code>#or
sd(c(3,6,12,89))</code></pre>
<pre><code>## [1] 41.17038</code></pre>
</div>
</div>
<p>Writing functions in R is pretty easy. Let’s create one to calculate the mean of a vector by getting the sum and length. First think about how to break it down into parts:</p>
<pre class="r"><code>x1= sum(x)
x2=length(x)
x1/x2</code></pre>
<pre><code>## [1] 25.125</code></pre>
<p>Then put it all back together and create a new function called <code>mymean</code>:</p>
<pre class="r"><code>mymean=function(f){
sum(f)/length(f)
}
mymean(f=x)</code></pre>
<pre><code>## [1] 25.125</code></pre>
<p>Confirm it works:</p>
<pre class="r"><code>mean(x)</code></pre>
<pre><code>## [1] 25.125</code></pre>
<div class="well">
Any potential problems with the <code>mymean</code> function?
</div>
</div>
<div id="missing-data-dealing-with-na-values" class="section level1">
<h1>Missing data: dealing with <code>NA</code> values</h1>
<pre class="r"><code>x3=c(5,8,NA,91,3,NA,14,30,100)</code></pre>
<div class="well">
" What do you think <code>mymean(x3)</code> will return?
</div>
<p>Calculate the mean using the new function</p>
<pre class="r"><code>mymean(x3)</code></pre>
<pre><code>## [1] NA</code></pre>
<p>Use the built-in function (with and without na.rm=T)</p>
<pre class="r"><code>mean(x3)</code></pre>
<pre><code>## [1] NA</code></pre>
<pre class="r"><code>mean(x3,na.rm=T)</code></pre>
<pre><code>## [1] 35.85714</code></pre>
<p>Writing simple functions is easy, writing robust, reliable functions can be hard…</p>
<div id="logical-values" class="section level2">
<h2>Logical values</h2>
<p>R also has standard conditional tests to generate <code>TRUE</code> or <code>FALSE</code> values (which also behave as <code>0</code>s and <code>1</code>s. These are often useful for filtering data (e.g. identify all values greater than 5). The logical operators are <code><</code>, <code><=</code>, <code>></code>, <code>>=</code>, <code>==</code> for exact equality and <code>!=</code> for inequality.</p>
<pre class="r"><code> x</code></pre>
<pre><code>## [1] 5 8 14 91 3 36 14 30</code></pre>
<pre class="r"><code> x3 > 75</code></pre>
<pre><code>## [1] FALSE FALSE NA TRUE FALSE NA FALSE FALSE TRUE</code></pre>
<pre class="r"><code> x3 == 40</code></pre>
<pre><code>## [1] FALSE FALSE NA FALSE FALSE NA FALSE FALSE FALSE</code></pre>
<pre class="r"><code> x3 > 15</code></pre>
<pre><code>## [1] FALSE FALSE NA TRUE FALSE NA FALSE TRUE TRUE</code></pre>
<p>And you can perform operations on those results:</p>
<pre class="r"><code>sum(x3>15,na.rm=T)</code></pre>
<pre><code>## [1] 3</code></pre>
<p>or save the results as variables:</p>
<pre class="r"><code>result = x3 > 3
result</code></pre>
<pre><code>## [1] TRUE TRUE NA TRUE FALSE NA TRUE TRUE TRUE</code></pre>
<div class="well">
<p>Define a function that counts how many values in a vector are less than or equal (<code><=</code>) to 12.</p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo3">
Show Solution
</button>
<div id="demo3" class="collapse">
<pre class="r"><code>mycount=function(x){
sum(x<=12)
}</code></pre>
<p>Try it:</p>
<pre class="r"><code>x3</code></pre>
<pre><code>## [1] 5 8 NA 91 3 NA 14 30 100</code></pre>
<pre class="r"><code>mycount(x3)</code></pre>
<pre><code>## [1] NA</code></pre>
<p>oops!</p>
<pre class="r"><code>mycount=function(x){
sum(x<=12,na.rm=T)
}</code></pre>
<p>Try it:</p>
<pre class="r"><code>x3</code></pre>
<pre><code>## [1] 5 8 NA 91 3 NA 14 30 100</code></pre>
<pre class="r"><code>mycount(x3)</code></pre>
<pre><code>## [1] 3</code></pre>
<p>Nice!</p>
</div>
</div>
</div>
</div>
<div id="generating-data" class="section level1">
<h1>Generating Data</h1>
<p>There are many ways to generate data in R such as sequences:</p>
<pre class="r"><code>seq(from=0, to=1, by=0.25)</code></pre>
<pre><code>## [1] 0.00 0.25 0.50 0.75 1.00</code></pre>
<p>and random numbers that follow a statistical distribution (such as the normal):</p>
<pre class="r"><code>a=rnorm(100,mean=0,sd=10)</code></pre>
<p>Let’s visualize those values in a histogram:</p>
<pre class="r"><code>hist(a)</code></pre>
<p><img src="01_Rintro_files/figure-html/unnamed-chunk-29-1.png" /><!-- --></p>
<p>We’ll cover much more sophisticated graphics later…</p>
</div>
<div id="data-types" class="section level1">
<h1>Data Types</h1>
<div id="matrices" class="section level2">
<h2>Matrices</h2>
<p>You can also use matrices (2-dimensional arrays of numbers):</p>
<pre class="r"><code>y=matrix(1:9,ncol=3)
y</code></pre>
<pre><code>## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9</code></pre>
<p>Matrices behave much like vectors:</p>
<pre class="r"><code>y+2</code></pre>
<pre><code>## [,1] [,2] [,3]
## [1,] 3 6 9
## [2,] 4 7 10
## [3,] 5 8 11</code></pre>
<p>and have 2-dimensional indexing:</p>
<pre class="r"><code>y[2,3]</code></pre>
<pre><code>## [1] 8</code></pre>
<div class="well">
<p>Create a 3x3 matrix full of random numbers. Hint: <code>rnorm(5)</code> will generate 5 random numbers</p>
<button data-toggle="collapse" class="btn btn-primary btn-sm round" data-target="#demo4">
Show Solution
</button>
<div id="demo4" class="collapse">
<pre class="r"><code>matrix(rnorm(9),nrow=3)</code></pre>
<pre><code>## [,1] [,2] [,3]
## [1,] 0.05710626 -1.15878801 0.2178735
## [2,] 0.90820862 0.01480023 0.2220378
## [3,] -0.40504985 1.23441876 0.8151335</code></pre>
</div>
</div>
</div>
<div id="data-frames" class="section level2">
<h2>Data Frames</h2>
<p>Data frames are similar to matrices, but more flexible. Matrices must be all the same type (e.g. all numbers), while a data frame can include multiple data types (e.g. text, factors, numbers). Dataframes are commonly used when doing statistical modeling in R.</p>
<pre class="r"><code>data = data.frame( x = c(11,12,14),
y = c("a","b","b"),
z = c(T,F,T))
data</code></pre>
<pre><code>## x y z
## 1 11 a TRUE
## 2 12 b FALSE
## 3 14 b TRUE</code></pre>
<p>You can subset in several ways</p>
<pre class="r"><code>mean(data$x)</code></pre>
<pre><code>## [1] 12.33333</code></pre>
<pre class="r"><code>mean(data[["x"]])</code></pre>
<pre><code>## [1] 12.33333</code></pre>
<pre class="r"><code>mean(data[,1])</code></pre>
<pre><code>## [1] 12.33333</code></pre>
</div>
</div>
<div id="loading-packages" class="section level1">
<h1>Loading Packages</h1>
<p>For installed packages: <code>library(packagename)</code>.</p>
<p>New packages: <code>install.packages()</code> or use the package manager.</p>
<pre class="r"><code>library(ggplot2)</code></pre>
<blockquote>
<p>R may ask you to choose a CRAN mirror. CRAN is the distributed network of servers that provides access to R’s software. It doesn’t really matter which you chose, but closer ones are likely to be faster. From RStudio, you can select the mirror under Tools→Options or just wait until it asks you.</p>
</blockquote>
<p>If you don’t have the packages above, install them in the package manager or by running <code>install.packages("raster")</code>.</p>
</div>
<div id="todays-task" class="section level1">
<h1>Today’s task</h1>
<p>Now <a href="CS_01.html">complete the first task here</a> by yourself or in small groups.</p>
</div>
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-2684666-2', 'auto');
ga('send', 'pageview');
</script>
<!-- disqus -->
<div id="disqus_thread" class="standardPadding"></div>
<script type="text/javascript" src="js/disqus.js"></script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<!-- disqus
<div id="disqus_thread" style="margin-top: 45px;"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'rdataanalysis'; // required: replace example with your forum shortname
var disqus_identifier = "{{ page.url }}";
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq );
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">
comments powered by Disqus.</a>
</noscript>
-->
<!-- give the footer some space -->
<br/>
<br/>
<footer id="site-footer">
<div id="footer1">
<a href="http://www.adamwilson.us"><img src="img/wilson.png" alt="logo" width=40px></a>
<a href="http://adamwilson.us/#contact"><i class="fa fa-envelope fa-2x"></i></a>
<a href="https://twitter.com/AdamWilsonLab"><i class="fa fa-twitter fa-2x"></i></a>
<a href="https://github.com/AdamMWilson"><i class="fa fa-github fa-2x"></i></a>
</div>
<div id="footer2">
<a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-sa/4.0/88x31.png" /></a>
</div>
</footer>
</div>
</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>
</body>
</html>