-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathr-data-types.html
465 lines (424 loc) · 32.9 KB
/
r-data-types.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
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>4 R Data types | Programming for Data Analysis and Statistics with R</title>
<meta name="description" content="4 R Data types | Programming for Data Analysis and Statistics with R" />
<meta name="generator" content="bookdown 0.20 and GitBook 2.6.7" />
<meta property="og:title" content="4 R Data types | Programming for Data Analysis and Statistics with R" />
<meta property="og:type" content="book" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="4 R Data types | Programming for Data Analysis and Statistics with R" />
<meta name="author" content="Diren Senger" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="rstudio-programming-environment.html"/>
<link rel="next" href="preparing-your-data-for-analysis.html"/>
<script src="book_assets/jquery-2.2.3/jquery.min.js"></script>
<link href="book_assets/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-table.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="book_assets/gitbook-2.6.7/css/plugin-clipboard.css" rel="stylesheet" />
<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li class="chapter" data-level="1" data-path="about.html"><a href="about.html"><i class="fa fa-check"></i><b>1</b> About</a></li>
<li class="chapter" data-level="2" data-path="installation-notes.html"><a href="installation-notes.html"><i class="fa fa-check"></i><b>2</b> Installation Notes</a></li>
<li class="chapter" data-level="3" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html"><i class="fa fa-check"></i><b>3</b> RStudio Programming Environment</a><ul>
<li class="chapter" data-level="3.1" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html#creating-a-new-project"><i class="fa fa-check"></i><b>3.1</b> Creating a new project</a></li>
<li class="chapter" data-level="3.2" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html#rstudio-screen"><i class="fa fa-check"></i><b>3.2</b> RStudio screen</a></li>
<li class="chapter" data-level="3.3" data-path="rstudio-programming-environment.html"><a href="rstudio-programming-environment.html#r-workflow"><i class="fa fa-check"></i><b>3.3</b> R Workflow</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="r-data-types.html"><a href="r-data-types.html"><i class="fa fa-check"></i><b>4</b> R Data types</a><ul>
<li class="chapter" data-level="4.1" data-path="r-data-types.html"><a href="r-data-types.html#character"><i class="fa fa-check"></i><b>4.1</b> Character</a></li>
<li class="chapter" data-level="4.2" data-path="r-data-types.html"><a href="r-data-types.html#numeric"><i class="fa fa-check"></i><b>4.2</b> Numeric</a></li>
<li class="chapter" data-level="4.3" data-path="r-data-types.html"><a href="r-data-types.html#logical"><i class="fa fa-check"></i><b>4.3</b> Logical</a></li>
<li class="chapter" data-level="4.4" data-path="r-data-types.html"><a href="r-data-types.html#vectors"><i class="fa fa-check"></i><b>4.4</b> Vectors</a></li>
<li class="chapter" data-level="4.5" data-path="r-data-types.html"><a href="r-data-types.html#factors"><i class="fa fa-check"></i><b>4.5</b> Factors</a></li>
<li class="chapter" data-level="4.6" data-path="r-data-types.html"><a href="r-data-types.html#list-of-lists"><i class="fa fa-check"></i><b>4.6</b> List of lists</a></li>
<li class="chapter" data-level="4.7" data-path="r-data-types.html"><a href="r-data-types.html#matrices"><i class="fa fa-check"></i><b>4.7</b> Matrices</a></li>
<li class="chapter" data-level="4.8" data-path="r-data-types.html"><a href="r-data-types.html#data-frames"><i class="fa fa-check"></i><b>4.8</b> Data frames</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html"><i class="fa fa-check"></i><b>5</b> Preparing your data for analysis</a><ul>
<li class="chapter" data-level="5.1" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#reading-the-data"><i class="fa fa-check"></i><b>5.1</b> Reading the data</a></li>
<li class="chapter" data-level="5.2" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#transforming-entire-columns"><i class="fa fa-check"></i><b>5.2</b> Transforming entire columns</a></li>
<li class="chapter" data-level="5.3" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#subsetting-data"><i class="fa fa-check"></i><b>5.3</b> Subsetting data</a></li>
<li class="chapter" data-level="5.4" data-path="preparing-your-data-for-analysis.html"><a href="preparing-your-data-for-analysis.html#useful-functions"><i class="fa fa-check"></i><b>5.4</b> Useful functions</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="conditions-if-and-else.html"><a href="conditions-if-and-else.html"><i class="fa fa-check"></i><b>6</b> Conditions - If and Else</a><ul>
<li class="chapter" data-level="6.1" data-path="conditions-if-and-else.html"><a href="conditions-if-and-else.html#if"><i class="fa fa-check"></i><b>6.1</b> If</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="for-and-while.html"><a href="for-and-while.html"><i class="fa fa-check"></i><b>7</b> For and While</a><ul>
<li class="chapter" data-level="7.1" data-path="for-and-while.html"><a href="for-and-while.html#for"><i class="fa fa-check"></i><b>7.1</b> For</a></li>
<li class="chapter" data-level="7.2" data-path="for-and-while.html"><a href="for-and-while.html#while"><i class="fa fa-check"></i><b>7.2</b> While</a></li>
<li class="chapter" data-level="7.3" data-path="for-and-while.html"><a href="for-and-while.html#do-while"><i class="fa fa-check"></i><b>7.3</b> Do-while</a></li>
<li class="chapter" data-level="7.4" data-path="for-and-while.html"><a href="for-and-while.html#for-vs.while"><i class="fa fa-check"></i><b>7.4</b> For vs. While</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="writing-own-functions.html"><a href="writing-own-functions.html"><i class="fa fa-check"></i><b>8</b> Writing own functions</a></li>
<li class="chapter" data-level="9" data-path="debugging.html"><a href="debugging.html"><i class="fa fa-check"></i><b>9</b> Debugging</a></li>
<li class="chapter" data-level="10" data-path="piping.html"><a href="piping.html"><i class="fa fa-check"></i><b>10</b> Piping</a></li>
<li class="chapter" data-level="11" data-path="plotting.html"><a href="plotting.html"><i class="fa fa-check"></i><b>11</b> Plotting</a><ul>
<li class="chapter" data-level="11.1" data-path="plotting.html"><a href="plotting.html#standard-library"><i class="fa fa-check"></i><b>11.1</b> Standard library</a><ul>
<li class="chapter" data-level="11.1.1" data-path="plotting.html"><a href="plotting.html#points-or-lines-type"><i class="fa fa-check"></i><b>11.1.1</b> Points or Lines? (type)</a></li>
<li class="chapter" data-level="11.1.2" data-path="plotting.html"><a href="plotting.html#line-type-lty"><i class="fa fa-check"></i><b>11.1.2</b> Line type (lty)</a></li>
</ul></li>
<li class="chapter" data-level="11.2" data-path="plotting.html"><a href="plotting.html#ggplot"><i class="fa fa-check"></i><b>11.2</b> ggplot</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="r-markdown.html"><a href="r-markdown.html"><i class="fa fa-check"></i><b>12</b> R-Markdown</a><ul>
<li class="chapter" data-level="12.1" data-path="r-markdown.html"><a href="r-markdown.html#output-formats"><i class="fa fa-check"></i><b>12.1</b> Output formats</a></li>
<li class="chapter" data-level="12.2" data-path="r-markdown.html"><a href="r-markdown.html#document-structure"><i class="fa fa-check"></i><b>12.2</b> Document structure</a></li>
<li class="chapter" data-level="12.3" data-path="r-markdown.html"><a href="r-markdown.html#code-blocks"><i class="fa fa-check"></i><b>12.3</b> Code blocks</a></li>
<li class="chapter" data-level="12.4" data-path="r-markdown.html"><a href="r-markdown.html#lists"><i class="fa fa-check"></i><b>12.4</b> Lists</a></li>
<li class="chapter" data-level="12.5" data-path="r-markdown.html"><a href="r-markdown.html#insert-an-image"><i class="fa fa-check"></i><b>12.5</b> Insert an image</a></li>
<li class="chapter" data-level="12.6" data-path="r-markdown.html"><a href="r-markdown.html#further-documentation"><i class="fa fa-check"></i><b>12.6</b> Further documentation</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html"><i class="fa fa-check"></i><b>13</b> Short statistical breaks</a><ul>
<li class="chapter" data-level="13.1" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#dinosours"><i class="fa fa-check"></i><b>13.1</b> Dinosours</a></li>
<li class="chapter" data-level="13.2" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#stars"><i class="fa fa-check"></i><b>13.2</b> Stars</a></li>
<li class="chapter" data-level="13.3" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#correlation"><i class="fa fa-check"></i><b>13.3</b> Correlation</a></li>
<li class="chapter" data-level="13.4" data-path="short-statistical-breaks.html"><a href="short-statistical-breaks.html#beginners-book"><i class="fa fa-check"></i><b>13.4</b> Beginners Book</a></li>
</ul></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Programming for Data Analysis and Statistics with R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="r-data-types" class="section level1">
<h1><span class="header-section-number">4</span> R Data types</h1>
<div id="character" class="section level2">
<h2><span class="header-section-number">4.1</span> Character</h2>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1">firstname <-<span class="st"> "Diren"</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2">surname <-<span class="st"> "Senger"</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">name <-<span class="st"> </span><span class="kw">paste</span>(firstname, surname)</a>
<a class="sourceLine" id="cb2-4" data-line-number="4">name</a></code></pre></div>
<pre><code>## [1] "Diren Senger"</code></pre>
</div>
<div id="numeric" class="section level2">
<h2><span class="header-section-number">4.2</span> Numeric</h2>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1">a <-<span class="st"> </span><span class="dv">1</span></a>
<a class="sourceLine" id="cb4-2" data-line-number="2">b <-<span class="st"> </span><span class="dv">2</span></a>
<a class="sourceLine" id="cb4-3" data-line-number="3">c <-<span class="st"> </span>a <span class="op">+</span><span class="st"> </span>b</a>
<a class="sourceLine" id="cb4-4" data-line-number="4">c</a></code></pre></div>
<pre><code>## [1] 3</code></pre>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1">d <-<span class="st"> </span>b<span class="op">*</span>c<span class="op">/</span>a</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">d</a></code></pre></div>
<pre><code>## [1] 6</code></pre>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">e <-<span class="st"> </span>d<span class="op">^</span><span class="dv">2</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2">e</a></code></pre></div>
<pre><code>## [1] 36</code></pre>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1">f <-<span class="st"> </span><span class="kw">as.integer</span>(<span class="fl">3.14</span>)</a>
<a class="sourceLine" id="cb10-2" data-line-number="2">f</a></code></pre></div>
<pre><code>## [1] 3</code></pre>
</div>
<div id="logical" class="section level2">
<h2><span class="header-section-number">4.3</span> Logical</h2>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1">a <-<span class="st"> </span><span class="ot">TRUE</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2">b <-<span class="st"> </span><span class="ot">FALSE</span></a>
<a class="sourceLine" id="cb12-3" data-line-number="3"></a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="co"># or</span></a>
<a class="sourceLine" id="cb12-5" data-line-number="5">a<span class="op">|</span>b</a></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="co"># and</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">a<span class="op">&</span>b</a></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1"><span class="co"># not</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2"><span class="op">!</span>a</a></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb18-1" data-line-number="1"><span class="dv">4</span> <span class="op">==</span><span class="st"> </span><span class="dv">5</span></a></code></pre></div>
<pre><code>## [1] FALSE</code></pre>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="dv">4</span> <span class="op"><</span><span class="st"> </span><span class="dv">5</span></a></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="dv">4</span><span class="op">>=</span><span class="dv">4</span></a></code></pre></div>
<pre><code>## [1] TRUE</code></pre>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb24-1" data-line-number="1">is.na</a></code></pre></div>
<pre><code>## function (x) .Primitive("is.na")</code></pre>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb26-1" data-line-number="1"><span class="kw">as.numeric</span>(a)</a></code></pre></div>
<pre><code>## [1] 1</code></pre>
</div>
<div id="vectors" class="section level2">
<h2><span class="header-section-number">4.4</span> Vectors</h2>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb28-1" data-line-number="1"><span class="co"># a simple numeric vector</span></a>
<a class="sourceLine" id="cb28-2" data-line-number="2">myvector <-<span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">4</span>,<span class="dv">6</span>)</a>
<a class="sourceLine" id="cb28-3" data-line-number="3">myvector</a></code></pre></div>
<pre><code>## [1] 1 4 6</code></pre>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb30-1" data-line-number="1"><span class="co"># a vector of type character</span></a>
<a class="sourceLine" id="cb30-2" data-line-number="2">myfamily <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Paula"</span>, <span class="st">"Ali"</span>, <span class="st">"Emma"</span>)</a>
<a class="sourceLine" id="cb30-3" data-line-number="3">myfamily</a></code></pre></div>
<pre><code>## [1] "Paula" "Ali" "Emma"</code></pre>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb32-1" data-line-number="1"><span class="co"># get the first element of a vector</span></a>
<a class="sourceLine" id="cb32-2" data-line-number="2">myfamily[<span class="dv">1</span>]</a></code></pre></div>
<pre><code>## [1] "Paula"</code></pre>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb34-1" data-line-number="1"><span class="co"># apply function to all items:</span></a>
<a class="sourceLine" id="cb34-2" data-line-number="2">myvector <span class="op">+</span><span class="st"> </span><span class="dv">1</span></a></code></pre></div>
<pre><code>## [1] 2 5 7</code></pre>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb36-1" data-line-number="1"><span class="kw">paste</span>(myfamily, <span class="st">"Meier"</span>)</a></code></pre></div>
<pre><code>## [1] "Paula Meier" "Ali Meier" "Emma Meier"</code></pre>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb38-1" data-line-number="1"><span class="co"># concatenate vectors:</span></a>
<a class="sourceLine" id="cb38-2" data-line-number="2">longervector <-<span class="st"> </span><span class="kw">c</span>(myvector, <span class="dv">5</span><span class="op">:</span><span class="dv">7</span>)</a>
<a class="sourceLine" id="cb38-3" data-line-number="3">longervector</a></code></pre></div>
<pre><code>## [1] 1 4 6 5 6 7</code></pre>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb40-1" data-line-number="1"><span class="co"># create a sequence</span></a>
<a class="sourceLine" id="cb40-2" data-line-number="2">odd <-<span class="st"> </span><span class="kw">seq</span>(<span class="dt">from =</span> <span class="dv">1</span>, <span class="dt">to =</span> <span class="dv">10</span>, <span class="dt">by =</span> <span class="dv">2</span>)</a>
<a class="sourceLine" id="cb40-3" data-line-number="3">odd</a></code></pre></div>
<pre><code>## [1] 1 3 5 7 9</code></pre>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb42-1" data-line-number="1"><span class="co"># create a boring sequence</span></a>
<a class="sourceLine" id="cb42-2" data-line-number="2">boring <-<span class="st"> </span><span class="kw">rep</span>(<span class="dv">1</span>, <span class="dv">10</span>)</a>
<a class="sourceLine" id="cb42-3" data-line-number="3">boring</a></code></pre></div>
<pre><code>## [1] 1 1 1 1 1 1 1 1 1 1</code></pre>
</div>
<div id="factors" class="section level2">
<h2><span class="header-section-number">4.5</span> Factors</h2>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb44-1" data-line-number="1">fac <-<span class="st"> </span><span class="kw">factor</span>(<span class="kw">c</span>(<span class="st">"good"</span>, <span class="st">"better"</span>, <span class="st">"best"</span>))</a>
<a class="sourceLine" id="cb44-2" data-line-number="2"><span class="kw">levels</span>(fac)</a></code></pre></div>
<pre><code>## [1] "best" "better" "good"</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb46-1" data-line-number="1"><span class="kw">nlevels</span>(fac)</a></code></pre></div>
<pre><code>## [1] 3</code></pre>
</div>
<div id="list-of-lists" class="section level2">
<h2><span class="header-section-number">4.6</span> List of lists</h2>
<p>You can create a list of lists (a list of vectors):</p>
<div class="sourceCode" id="cb48"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb48-1" data-line-number="1">myList <-<span class="st"> </span><span class="kw">list</span>(<span class="dt">smallLetters=</span>letters[<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>], </a>
<a class="sourceLine" id="cb48-2" data-line-number="2"> <span class="dt">capitalLetters=</span>LETTERS[<span class="dv">1</span><span class="op">:</span><span class="dv">10</span>], </a>
<a class="sourceLine" id="cb48-3" data-line-number="3"> <span class="dt">numbers=</span><span class="dv">1</span><span class="op">:</span><span class="dv">5</span>)</a>
<a class="sourceLine" id="cb48-4" data-line-number="4">myList</a></code></pre></div>
<pre><code>## $smallLetters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
##
## $capitalLetters
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"
##
## $numbers
## [1] 1 2 3 4 5</code></pre>
<p>If you want to choose lists, you can do it with a single bracket.</p>
<div class="sourceCode" id="cb50"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb50-1" data-line-number="1"><span class="co"># Accessing multiple elements</span></a>
<a class="sourceLine" id="cb50-2" data-line-number="2">myList[<span class="dv">1</span><span class="op">:</span><span class="dv">2</span>]</a></code></pre></div>
<pre><code>## $smallLetters
## [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
##
## $capitalLetters
## [1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J"</code></pre>
<p>If you want to choose single elements, you need double brackets:</p>
<div class="sourceCode" id="cb52"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb52-1" data-line-number="1"><span class="co"># Accessing single elements</span></a>
<a class="sourceLine" id="cb52-2" data-line-number="2">myList[<span class="dv">2</span>][<span class="dv">2</span>]</a></code></pre></div>
<pre><code>## $<NA>
## NULL</code></pre>
<div class="sourceCode" id="cb54"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb54-1" data-line-number="1">myList[[<span class="dv">2</span>]][<span class="dv">2</span>]</a></code></pre></div>
<pre><code>## [1] "B"</code></pre>
<div class="sourceCode" id="cb56"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb56-1" data-line-number="1">myList[[<span class="dv">1</span><span class="op">:</span><span class="dv">2</span>]]</a></code></pre></div>
<pre><code>## [1] "b"</code></pre>
</div>
<div id="matrices" class="section level2">
<h2><span class="header-section-number">4.7</span> Matrices</h2>
<div class="sourceCode" id="cb58"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb58-1" data-line-number="1">m <-<span class="st"> </span><span class="kw">matrix</span>(<span class="dt">data=</span><span class="dv">1</span><span class="op">:</span><span class="dv">12</span>, <span class="dt">nrow =</span> <span class="dv">3</span>, <span class="dt">ncol =</span> <span class="dv">4</span>)</a>
<a class="sourceLine" id="cb58-2" data-line-number="2">m</a></code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12</code></pre>
<p>Element wise operations:</p>
<div class="sourceCode" id="cb60"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb60-1" data-line-number="1">m</a></code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12</code></pre>
<div class="sourceCode" id="cb62"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb62-1" data-line-number="1"><span class="co"># Element-wise operations</span></a>
<a class="sourceLine" id="cb62-2" data-line-number="2">m <span class="op">*</span><span class="st"> </span><span class="fl">0.5</span></a></code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 0.5 2.0 3.5 5.0
## [2,] 1.0 2.5 4.0 5.5
## [3,] 1.5 3.0 4.5 6.0</code></pre>
<div class="sourceCode" id="cb64"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb64-1" data-line-number="1">m</a></code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12</code></pre>
<div class="sourceCode" id="cb66"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb66-1" data-line-number="1"><span class="co"># Element-wise operations</span></a>
<a class="sourceLine" id="cb66-2" data-line-number="2">m <span class="op">*</span><span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>)</a></code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 4 10 16 22
## [3,] 9 18 27 36</code></pre>
<p>matrix/ vector multiplication</p>
<div class="sourceCode" id="cb68"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb68-1" data-line-number="1">m</a></code></pre></div>
<pre><code>## [,1] [,2] [,3] [,4]
## [1,] 1 4 7 10
## [2,] 2 5 8 11
## [3,] 3 6 9 12</code></pre>
<div class="sourceCode" id="cb70"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb70-1" data-line-number="1"><span class="co"># Matrix multiplication</span></a>
<a class="sourceLine" id="cb70-2" data-line-number="2">m <span class="op">%*%</span><span class="st"> </span><span class="kw">c</span>(<span class="dv">1</span>,<span class="dv">2</span>,<span class="dv">3</span>,<span class="dv">4</span>)</a></code></pre></div>
<pre><code>## [,1]
## [1,] 70
## [2,] 80
## [3,] 90</code></pre>
<p>Good explanation of Matrix multiplication:
<a href="https://eli.thegreenplace.net/2015/visualizing-matrix-multiplication-as-a-linear-combination/">Eli Bendersky’s website</a></p>
</div>
<div id="data-frames" class="section level2">
<h2><span class="header-section-number">4.8</span> Data frames</h2>
<div class="sourceCode" id="cb72"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb72-1" data-line-number="1"><span class="co"># Creating a data frame</span></a>
<a class="sourceLine" id="cb72-2" data-line-number="2">family.frame <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">index =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">3</span>, <span class="dt">firstname =</span> myfamily, <span class="dt">surname =</span> <span class="kw">rep</span>(<span class="st">"Meier"</span>, <span class="dv">3</span>))</a></code></pre></div>
<div class="sourceCode" id="cb73"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb73-1" data-line-number="1"><span class="kw">library</span>(knitr)</a>
<a class="sourceLine" id="cb73-2" data-line-number="2"><span class="kw">kable</span>(family.frame)</a></code></pre></div>
<table>
<thead>
<tr class="header">
<th align="right">index</th>
<th align="left">firstname</th>
<th align="left">surname</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="right">1</td>
<td align="left">Paula</td>
<td align="left">Meier</td>
</tr>
<tr class="even">
<td align="right">2</td>
<td align="left">Ali</td>
<td align="left">Meier</td>
</tr>
<tr class="odd">
<td align="right">3</td>
<td align="left">Emma</td>
<td align="left">Meier</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="rstudio-programming-environment.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="preparing-your-data-for-analysis.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="book_assets/gitbook-2.6.7/js/app.min.js"></script>
<script src="book_assets/gitbook-2.6.7/js/lunr.js"></script>
<script src="book_assets/gitbook-2.6.7/js/clipboard.min.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="book_assets/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script src="book_assets/gitbook-2.6.7/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": null,
"text": null
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "subsection"
},
"search": false
});
});
</script>
</body>
</html>