-
Notifications
You must be signed in to change notification settings - Fork 31k
Expand file tree
/
Copy pathindex.html
More file actions
641 lines (544 loc) · 17.6 KB
/
index.html
File metadata and controls
641 lines (544 loc) · 17.6 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
<!DOCTYPE html>
<html>
<head>
<title>Introduction to the R Language</title>
<meta charset="utf-8">
<meta name="description" content="Introduction to the R Language">
<meta name="author" content="Roger Peng, Associate Professor">
<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 = "../../assets/css/custom.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/bloomberg_shield.png">
</aside>
<hgroup class="auto-fadein">
<h1>Introduction to the R Language</h1>
<h2>Data Types and Basic Operations</h2>
<p>Roger Peng, Associate Professor<br/>Johns Hopkins Bloomberg School of Public Health</p>
</hgroup>
</slide>
<!-- SLIDES -->
<slide class="" id="slide-1" style="background:;">
<hgroup>
<h2>Objects</h2>
</hgroup>
<article>
<p>R has five basic or “atomic” classes of objects:</p>
<ul>
<li><p>character</p></li>
<li><p>numeric (real numbers)</p></li>
<li><p>integer</p></li>
<li><p>complex</p></li>
<li><p>logical (True/False)</p></li>
</ul>
<p>The most basic object is a vector</p>
<ul>
<li><p>A vector can only contain objects of the same class</p></li>
<li><p>BUT: The one exception is a <em>list</em>, which is represented as a vector
but can contain objects of different classes (indeed, that’s usually
why we use them)</p></li>
</ul>
<p>Empty vectors can be created with the <code>vector()</code> function.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-2" style="background:;">
<hgroup>
<h2>Numbers</h2>
</hgroup>
<article>
<ul>
<li><p>Numbers in R a generally treated as numeric objects (i.e. double
precision real numbers)</p></li>
<li><p>If you explicitly want an integer, you need to specify the <code>L</code>
suffix</p></li>
<li><p>Ex: Entering <code>1</code> gives you a numeric object; entering <code>1L</code>
explicitly gives you an integer.</p></li>
<li><p>There is also a special number <code>Inf</code> which represents infinity;
e.g. <code>1 / 0</code>; <code>Inf</code> can be used in ordinary calculations;
e.g. <code>1 / Inf</code> is 0</p></li>
<li><p>The value <code>NaN</code> represents an undefined value (“not a number”);
e.g. 0 / 0; <code>NaN</code> can also be thought of as a missing value
(more on that later)</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-3" style="background:;">
<hgroup>
<h2>Attributes</h2>
</hgroup>
<article>
<p>R objects can have attributes</p>
<ul>
<li><p>names, dimnames</p></li>
<li><p>dimensions (e.g. matrices, arrays)</p></li>
<li><p>class</p></li>
<li><p>length</p></li>
<li><p>other user-defined attributes/metadata</p></li>
</ul>
<p>Attributes of an object can be accessed using the <code>attributes()</code>
function.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-4" style="background:;">
<hgroup>
<h2>Entering Input</h2>
</hgroup>
<article>
<p>At the R prompt we type expressions. The <code><-</code> symbol is the assignment operator.</p>
<pre><code class="r">> x <- 1
> print(x)
[1] 1
> x
[1] 1
> msg <- "hello"
</code></pre>
<p>The grammar of the language determines whether an expression is complete or not.</p>
<pre><code class="r">> x <- ## Incomplete expression
</code></pre>
<p>The # character indicates a comment. Anything to the right of the # (including the # itself) is ignored.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-5" style="background:;">
<hgroup>
<h2>Evaluation</h2>
</hgroup>
<article>
<p>When a complete expression is entered at the prompt, it is evaluated and the result of the evaluated expression is returned. The result may be auto-printed.</p>
<pre><code class="r">> x <- 5 ## nothing printed
> x ## auto-printing occurs
[1] 5
> print(x) ## explicit printing
[1] 5
</code></pre>
<p>The <code>[1]</code> indicates that <code>x</code> is a vector and <code>5</code> is the first element.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-6" style="background:;">
<hgroup>
<h2>Printing</h2>
</hgroup>
<article>
<pre><code class="r">> x <- 1:20
> x
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
[16] 16 17 18 19 20
</code></pre>
<p>The <code>:</code> operator is used to create integer sequences.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-7" style="background:;">
<hgroup>
<h2>Creating Vectors</h2>
</hgroup>
<article>
<p>The <code>c()</code> function can be used to create vectors of objects.</p>
<pre><code class="r">> x <- c(0.5, 0.6) ## numeric
> x <- c(TRUE, FALSE) ## logical
> x <- c(T, F) ## logical
> x <- c("a", "b", "c") ## character
> x <- 9:29 ## integer
> x <- c(1+0i, 2+4i) ## complex
</code></pre>
<p>Using the <code>vector()</code> function</p>
<pre><code class="r">> x <- vector("numeric", length = 10)
> x
[1] 0 0 0 0 0 0 0 0 0 0
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-8" style="background:;">
<hgroup>
<h2>Mixing Objects</h2>
</hgroup>
<article>
<p>What about the following?</p>
<pre><code class="r">> y <- c(1.7, "a") ## character
> y <- c(TRUE, 2) ## numeric
> y <- c("a", TRUE) ## character
</code></pre>
<p>When different objects are mixed in a vector, <em>coercion</em> occurs so that every element in the vector is of the same class.</p>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-9" style="background:;">
<hgroup>
<h2>Explicit Coercion</h2>
</hgroup>
<article>
<p>Objects can be explicitly coerced from one class to another using the <code>as.*</code> functions, if available.</p>
<pre><code class="r">> x <- 0:6
> class(x)
[1] "integer"
> as.numeric(x)
[1] 0 1 2 3 4 5 6
> as.logical(x)
[1] FALSE TRUE TRUE TRUE TRUE TRUE TRUE
> as.character(x)
[1] "0" "1" "2" "3" "4" "5" "6"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-10" style="background:;">
<hgroup>
<h2>Explicit Coercion</h2>
</hgroup>
<article>
<p>Nonsensical coercion results in <code>NA</code>s.</p>
<pre><code class="r">> x <- c("a", "b", "c")
> as.numeric(x)
[1] NA NA NA
Warning message:
NAs introduced by coercion
> as.logical(x)
[1] NA NA NA
> as.complex(x)
[1] NA NA NA NA
Warning message:
NAs introduced by coercion
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-11" style="background:;">
<hgroup>
<h2>Matrices</h2>
</hgroup>
<article>
<p>Matrices are vectors with a <em>dimension</em> attribute. The dimension attribute is itself an integer vector of length 2 (nrow, ncol)</p>
<pre><code class="r">> m <- matrix(nrow = 2, ncol = 3)
> m
[,1] [,2] [,3]
[1,] NA NA NA
[2,] NA NA NA
> dim(m)
[1] 2 3
> attributes(m)
$dim
[1] 2 3
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-12" style="background:;">
<hgroup>
<h2>Matrices (cont’d)</h2>
</hgroup>
<article>
<p>Matrices are constructed <em>column-wise</em>, so entries can be thought of starting in the “upper left” corner and running down the columns.</p>
<pre><code class="r">> m <- matrix(1:6, nrow = 2, ncol = 3)
> m
[,1] [,2] [,3]
[1,] 1 3 5
[2,] 2 4 6
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-13" style="background:;">
<hgroup>
<h2>Matrices (cont’d)</h2>
</hgroup>
<article>
<p>Matrices can also be created directly from vectors by adding a dimension attribute.</p>
<pre><code class="r">> m <- 1:10
> m
[1] 1 2 3 4 5 6 7 8 9 10
> dim(m) <- c(2, 5)
> m
[,1] [,2] [,3] [,4] [,5]
[1,] 1 3 5 7 9
[2,] 2 4 6 8 10
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-14" style="background:;">
<hgroup>
<h2>cbind-ing and rbind-ing</h2>
</hgroup>
<article>
<p>Matrices can be created by <em>column-binding</em> or <em>row-binding</em> with <code>cbind()</code> and <code>rbind()</code>.</p>
<pre><code class="r">> x <- 1:3
> y <- 10:12
> cbind(x, y)
x y
[1,] 1 10
[2,] 2 11
[3,] 3 12
> rbind(x, y)
[,1] [,2] [,3]
x 1 2 3
y 10 11 12
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-15" style="background:;">
<hgroup>
<h2>Lists</h2>
</hgroup>
<article>
<p>Lists are a special type of vector that can contain elements of different classes. Lists are a very important data type in R and you should get to know them well.</p>
<pre><code class="r">> x <- list(1, "a", TRUE, 1 + 4i)
> x
[[1]]
[1] 1
[[2]]
[1] "a"
[[3]]
[1] TRUE
[[4]]
[1] 1+4i
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-16" style="background:;">
<hgroup>
<h2>Factors</h2>
</hgroup>
<article>
<p>Factors are used to represent categorical data. Factors can be unordered or ordered. One can think of a factor as an integer vector where each integer has a <em>label</em>.</p>
<ul>
<li><p>Factors are treated specially by modelling functions like <code>lm()</code> and <code>glm()</code></p></li>
<li><p>Using factors with labels is <em>better</em> than using integers because factors are self-describing; having a variable that has values “Male” and “Female” is better than a variable that has values 1 and 2.</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-17" style="background:;">
<hgroup>
<h2>Factors</h2>
</hgroup>
<article>
<pre><code class="r">> x <- factor(c("yes", "yes", "no", "yes", "no"))
> x
[1] yes yes no yes no
Levels: no yes
> table(x)
x
no yes
2 3
> unclass(x)
[1] 2 2 1 2 1
attr(,"levels")
[1] "no" "yes"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-18" style="background:;">
<hgroup>
<h2>Factors</h2>
</hgroup>
<article>
<p>The order of the levels can be set using the <code>levels</code> argument to <code>factor()</code>. This can be important in linear modelling because the first level is used as the baseline level.</p>
<pre><code class="r">> x <- factor(c("yes", "yes", "no", "yes", "no"),
levels = c("yes", "no"))
> x
[1] yes yes no yes no
Levels: yes no
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-19" style="background:;">
<hgroup>
<h2>Missing Values</h2>
</hgroup>
<article>
<p>Missing values are denoted by <code>NA</code> or <code>NaN</code> for undefined mathematical operations. </p>
<ul>
<li><p><code>is.na()</code> is used to test objects if they are <code>NA</code></p></li>
<li><p><code>is.nan()</code> is used to test for <code>NaN</code></p></li>
<li><p><code>NA</code> values have a class also, so there are integer <code>NA</code>, character <code>NA</code>, etc.</p></li>
<li><p>A <code>NaN</code> value is also <code>NA</code> but the converse is not true</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-20" style="background:;">
<hgroup>
<h2>Missing Values</h2>
</hgroup>
<article>
<pre><code class="r">> x <- c(1, 2, NA, 10, 3)
> is.na(x)
[1] FALSE FALSE TRUE FALSE FALSE
> is.nan(x)
[1] FALSE FALSE FALSE FALSE FALSE
> x <- c(1, 2, NaN, NA, 4)
> is.na(x)
[1] FALSE FALSE TRUE TRUE FALSE
> is.nan(x)
[1] FALSE FALSE TRUE FALSE FALSE
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-21" style="background:;">
<hgroup>
<h2>Data Frames</h2>
</hgroup>
<article>
<p>Data frames are used to store tabular data</p>
<ul>
<li><p>They are represented as a special type of list where every element of the list has to have the same length</p></li>
<li><p>Each element of the list can be thought of as a column and the length of each element of the list is the number of rows</p></li>
<li><p>Unlike matrices, data frames can store different classes of objects in each column (just like lists); matrices must have every element be the same class</p></li>
<li><p>Data frames also have a special attribute called <code>row.names</code></p></li>
<li><p>Data frames are usually created by calling <code>read.table()</code> or <code>read.csv()</code></p></li>
<li><p>Can be converted to a matrix by calling <code>data.matrix()</code></p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-22" style="background:;">
<hgroup>
<h2>Data Frames</h2>
</hgroup>
<article>
<pre><code class="r">> x <- data.frame(foo = 1:4, bar = c(T, T, F, F))
> x
foo bar
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 FALSE
> nrow(x)
[1] 4
> ncol(x)
[1] 2
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-23" style="background:;">
<hgroup>
<h2>Names</h2>
</hgroup>
<article>
<p>R objects can also have names, which is very useful for writing readable code and self-describing objects.</p>
<pre><code class="r">> x <- 1:3
> names(x)
NULL
> names(x) <- c("foo", "bar", "norf")
> x
foo bar norf
1 2 3
> names(x)
[1] "foo" "bar" "norf"
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-24" style="background:;">
<hgroup>
<h2>Names</h2>
</hgroup>
<article>
<p>Lists can also have names.</p>
<pre><code class="r">> x <- list(a = 1, b = 2, c = 3)
> x
$a
[1] 1
$b
[1] 2
$c
[1] 3
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-25" style="background:;">
<hgroup>
<h2>Names</h2>
</hgroup>
<article>
<p>And matrices.</p>
<pre><code class="r">> m <- matrix(1:4, nrow = 2, ncol = 2)
> dimnames(m) <- list(c("a", "b"), c("c", "d"))
> m
c d
a 1 3
b 2 4
</code></pre>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="" id="slide-26" style="background:;">
<hgroup>
<h2>Summary</h2>
</hgroup>
<article>
<p>Data Types</p>
<ul>
<li><p>atomic classes: numeric, logical, character, integer, complex \</p></li>
<li><p>vectors, lists</p></li>
<li><p>factors</p></li>
<li><p>missing values</p></li>
<li><p>data frames</p></li>
<li><p>names</p></li>
</ul>
</article>
<!-- Presenter Notes -->
</slide>
<slide class="backdrop"></slide>
</slides>
<!--[if IE]>
<script
src="http://ajax.googleapis.com/ajax/libs/chrome-frame/1/CFInstall.min.js">
</script>
<script>CFInstall.check({mode: 'overlay'});</script>
<![endif]-->
</body>
<!-- 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-1.7.min.js"><\/script>')</script>
<!-- Load Javascripts for Widgets -->
<!-- MathJax: Fall back to local if CDN offline but local image fonts are not supported (saves >100MB) -->
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [['$','$'], ['\\(','\\)']],
processEscapes: true
}
});
</script>
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<!-- <script src="https://c328740.ssl.cf1.rackcdn.com/mathjax/2.0-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script> -->
<script>window.MathJax || document.write('<script type="text/x-mathjax-config">MathJax.Hub.Config({"HTML-CSS":{imageFont:null}});<\/script><script src="../../libraries/widgets/mathjax/MathJax.js?config=TeX-AMS-MML_HTMLorMML"><\/script>')
</script>
<!-- LOAD HIGHLIGHTER JS FILES -->
<script src="../../libraries/highlighters/highlight.js/highlight.pack.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- DONE LOADING HIGHLIGHTER JS FILES -->
</html>