-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators-data-objects.html
522 lines (508 loc) · 39.1 KB
/
operators-data-objects.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
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>3 Operators & Data Objects | A Comprehensive Guide to Use R</title>
<meta name="author" content="Africano BYAMUGISHA">
<meta name="description" content="3.1 Operators R has a wide range of operators for performing mathematical and logical operations. In this section, we’ll cover arithmetic operators, logical operators, comparison operators, and...">
<meta name="generator" content="bookdown 0.33 with bs4_book()">
<meta property="og:title" content="3 Operators & Data Objects | A Comprehensive Guide to Use R">
<meta property="og:type" content="book">
<meta property="og:url" content="https://github.io/africanobyamugisha/operators-data-objects.html">
<meta property="og:image" content="https://github.io/africanobyamugisha/images/flyer.png">
<meta property="og:description" content="3.1 Operators R has a wide range of operators for performing mathematical and logical operations. In this section, we’ll cover arithmetic operators, logical operators, comparison operators, and...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="3 Operators & Data Objects | A Comprehensive Guide to Use R">
<meta name="twitter:description" content="3.1 Operators R has a wide range of operators for performing mathematical and logical operations. In this section, we’ll cover arithmetic operators, logical operators, comparison operators, and...">
<meta name="twitter:image" content="https://github.io/africanobyamugisha/images/flyer.png">
<!-- JS --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/fuse.js/6.4.6/fuse.js" integrity="sha512-zv6Ywkjyktsohkbp9bb45V6tEMoWhzFzXis+LrMehmJZZSys19Yxf1dopHx7WzIKxr5tK2dVcYmaCk2uqdjF4A==" crossorigin="anonymous"></script><script src="https://kit.fontawesome.com/6ecbd6c532.js" crossorigin="anonymous"></script><script src="libs/jquery-3.6.0/jquery-3.6.0.min.js"></script><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link href="libs/bootstrap-4.6.0/bootstrap.min.css" rel="stylesheet">
<script src="libs/bootstrap-4.6.0/bootstrap.bundle.min.js"></script><script src="libs/bs3compat-0.4.2/transition.js"></script><script src="libs/bs3compat-0.4.2/tabs.js"></script><script src="libs/bs3compat-0.4.2/bs3compat.js"></script><link href="libs/bs4_book-1.0.0/bs4_book.css" rel="stylesheet">
<script src="libs/bs4_book-1.0.0/bs4_book.js"></script><script src="libs/htmlwidgets-1.6.1/htmlwidgets.js"></script><script src="libs/plotly-binding-4.10.1/plotly.js"></script><script src="libs/typedarray-0.1/typedarray.min.js"></script><link href="libs/crosstalk-1.2.0/css/crosstalk.min.css" rel="stylesheet">
<script src="libs/crosstalk-1.2.0/js/crosstalk.min.js"></script><link href="libs/plotly-htmlwidgets-css-2.11.1/plotly-htmlwidgets.css" rel="stylesheet">
<script src="libs/plotly-main-2.11.1/plotly-latest.min.js"></script><script src="libs/kePrint-0.0.1/kePrint.js"></script><link href="libs/lightable-0.0.1/lightable.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/autocomplete.js/0.38.0/autocomplete.jquery.min.js" integrity="sha512-GU9ayf+66Xx2TmpxqJpliWbT5PiGYxpaG8rfnBEk1LL8l1KGkRShhngwdXK1UgqhAzWpZHSiYPc09/NwDQIGyg==" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/mark.min.js" integrity="sha512-5CYOlHXGh6QpOFA/TeTylKLWfB3ftPsde7AnmhuitiTX4K5SqCLBeKro6sPS8ilsz1Q4NRx3v8Ko2IBiszzdww==" crossorigin="anonymous"></script><!-- CSS --><style type="text/css">
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<link rel="stylesheet" href="bs4_style.css">
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container-fluid">
<div class="row">
<header class="col-sm-12 col-lg-3 sidebar sidebar-book"><a class="sr-only sr-only-focusable" href="#content">Skip to main content</a>
<div class="d-flex align-items-start justify-content-between">
<h1>
<a href="index.html" title="">A Comprehensive Guide to Use R</a>
</h1>
<button class="btn btn-outline-primary d-lg-none ml-2 mt-1" type="button" data-toggle="collapse" data-target="#main-nav" aria-expanded="true" aria-controls="main-nav"><i class="fas fa-bars"></i><span class="sr-only">Show table of contents</span></button>
</div>
<div id="main-nav" class="collapse-lg">
<form role="search">
<input id="search" class="form-control" type="search" placeholder="Search" aria-label="Search">
</form>
<nav aria-label="Table of contents"><h2>Table of contents</h2>
<ul class="book-toc list-unstyled">
<li><a class="" href="index.html">*</a></li>
<li><a class="" href="about-the-author.html">About the Author</a></li>
<li><a class="" href="about-this-guide.html">About this guide</a></li>
<li><a class="" href="introduction.html"><span class="header-section-number">1</span> Introduction</a></li>
<li><a class="" href="getting-started-setting-up.html"><span class="header-section-number">2</span> Getting Started & Setting Up</a></li>
<li><a class="active" href="operators-data-objects.html"><span class="header-section-number">3</span> Operators & Data Objects</a></li>
<li><a class="" href="working-with-data.html"><span class="header-section-number">4</span> Working With Data</a></li>
<li><a class="" href="data-wrangling.html"><span class="header-section-number">5</span> Data Wrangling</a></li>
<li><a class="" href="data-visualisation.html"><span class="header-section-number">6</span> Data Visualisation</a></li>
<li><a class="" href="statistics-in-r.html"><span class="header-section-number">7</span> Statistics in R</a></li>
<li><a class="" href="functions.html"><span class="header-section-number">8</span> Functions</a></li>
<li><a class="" href="reproducible-results-markdown-quarto-notebooks.html"><span class="header-section-number">9</span> Reproducible Results (Markdown, Quarto & Notebooks)</a></li>
<li><a class="" href="r-shiny.html"><span class="header-section-number">10</span> R Shiny</a></li>
<li><a class="" href="github_r.html"><span class="header-section-number">11</span> Version control with Git and GitHub</a></li>
<li><a class="" href="additional-resources.html">Additional Resources</a></li>
</ul>
<div class="book-extra">
</div>
</nav>
</div>
</header><main class="col-sm-12 col-md-9 col-lg-7" id="content"><div id="operators-data-objects" class="section level1" number="3">
<h1>
<span class="header-section-number">3</span> Operators & Data Objects<a class="anchor" aria-label="anchor" href="#operators-data-objects"><i class="fas fa-link"></i></a>
</h1>
<div class="inline-figure"><img src="images/operators.jpg" style="width:95.0%"></div>
<div id="operators" class="section level2" number="3.1">
<h2>
<span class="header-section-number">3.1</span> Operators<a class="anchor" aria-label="anchor" href="#operators"><i class="fas fa-link"></i></a>
</h2>
<p>R has a wide range of operators for performing mathematical and logical operations. In this section, we’ll cover arithmetic operators, logical operators, comparison operators, and more.</p>
<div id="assignment-operators" class="section level3" number="3.1.1">
<h3>
<span class="header-section-number">3.1.1</span> Assignment Operators<a class="anchor" aria-label="anchor" href="#assignment-operators"><i class="fas fa-link"></i></a>
</h3>
<p>Assignment operators are used to assigning values to various data objects in R. The objects may be integers, vectors, or functions. These values are then stored by the assigned variable names. There are two kinds of assignment operators: Left and Right</p>
<p><strong>Left Assignment (<code>\<-</code> or <code><<-</code> or <code>=</code>)</strong></p>
<p><strong>Right Assignment (<code>-></code> or <code>->></code>)</strong></p>
<p>The operators <code><-</code> and <code>=</code> can be used, almost interchangeably, to assign to variable in the same environment.</p>
<p>The <code><<-</code> operator is used for assigning to variables in the parent environments (more like global assignments).</p>
<p></p>
<div class="sourceCode" id="cb12"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">my_var</span> <span class="op"><-</span> <span class="fl">3</span></span>
<span><span class="va">my_var</span> <span class="op"><<-</span> <span class="fl">3</span></span>
<span><span class="fl">3</span> <span class="op">-></span> <span class="va">my_var</span></span>
<span><span class="va">my_var</span> <span class="op"><<-</span> <span class="fl">3</span></span>
<span><span class="va">my_var</span> <span class="co"># print my_var</span></span></code></pre></div>
<p></p>
<p><<code><-</code> OR <code>->></code>is a global assigner.</p>
<p>Normally, when you create a variable inside a function, that variable is local, and can only be used inside that function.</p>
<p>To create a global variable inside a function, you can use the <strong>global assignment</strong> operator.</p>
<p></p>
<div class="sourceCode" id="cb13"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">my_function</span> <span class="op"><-</span> <span class="kw">function</span><span class="op">(</span><span class="op">)</span> <span class="op">{</span></span>
<span> <span class="va">txt</span> <span class="op"><<-</span> <span class="st">"fantastic"</span></span>
<span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="st">"R is"</span>, <span class="va">txt</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span><span class="fu">my_function</span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">txt</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p>Additionally, use the <strong>global</strong> assignment operator if you want to change a global variable inside a function.</p>
<p></p>
<div class="sourceCode" id="cb14"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">txt</span> <span class="op"><-</span> <span class="st">"awesome"</span></span>
<span><span class="va">my_function</span> <span class="op"><-</span> <span class="kw">function</span><span class="op">(</span><span class="op">)</span> <span class="op">{</span></span>
<span> <span class="va">txt</span> <span class="op"><<-</span> <span class="st">"fantastic"</span></span>
<span> <span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="st">"R is"</span>, <span class="va">txt</span><span class="op">)</span></span>
<span><span class="op">}</span></span>
<span><span class="fu">my_function</span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/paste.html">paste</a></span><span class="op">(</span><span class="st">"R is"</span>, <span class="va">txt</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p><strong>However</strong>, don’t worry much on function and the global variable, You will learn more about this in the preceding chapters.</p>
</div>
<div id="arithmetic-operators" class="section level3" number="3.1.2">
<h3>
<span class="header-section-number">3.1.2</span> Arithmetic Operators<a class="anchor" aria-label="anchor" href="#arithmetic-operators"><i class="fas fa-link"></i></a>
</h3>
<p>Arithmetic operators are used with numeric values to perform common mathematical operations:</p>
<div class="inline-table"><table class="table table-sm"><tbody>
<tr class="odd">
<td><strong>Operators</strong></td>
<td><strong>Description</strong></td>
</tr>
<tr class="even">
<td>+</td>
<td>Addition</td>
</tr>
<tr class="odd">
<td>_</td>
<td>Subtraction</td>
</tr>
<tr class="even">
<td>*</td>
<td>Multiplication</td>
</tr>
<tr class="odd">
<td>/</td>
<td>Division</td>
</tr>
<tr class="even">
<td>^</td>
<td>Exponent</td>
</tr>
<tr class="odd">
<td>%</td>
<td>Modulus (Remainder from division)</td>
</tr>
<tr class="even">
<td>%/%</td>
<td>Integer Division</td>
</tr>
</tbody></table></div>
<p></p>
<div class="sourceCode" id="cb15"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">a</span> <span class="op"><-</span> <span class="fl">10</span></span>
<span><span class="va">b</span> <span class="op"><-</span> <span class="fl">4</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">a</span> <span class="op">+</span> <span class="va">b</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">a</span> <span class="op">-</span> <span class="va">b</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">a</span> <span class="op">*</span> <span class="va">b</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">a</span><span class="op">/</span><span class="va">b</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">a</span><span class="op">^</span><span class="va">b</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">a</span><span class="op"><a href="https://rdrr.io/r/base/Arithmetic.html">%/%</a></span><span class="va">b</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="relational-operators" class="section level3" number="3.1.3">
<h3>
<span class="header-section-number">3.1.3</span> Relational Operators<a class="anchor" aria-label="anchor" href="#relational-operators"><i class="fas fa-link"></i></a>
</h3>
<p>These are used for comparisons between values.</p>
<p>Each element of the first value is compared with the corresponding element of the second value.</p>
<p>The result of comparison is a Boolean value (TRUE/FALSE)</p>
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th>Operator</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>==</td>
<td>Equal</td>
</tr>
<tr class="even">
<td>!=</td>
<td>Not equal</td>
</tr>
<tr class="odd">
<td>></td>
<td>Greater than</td>
</tr>
<tr class="even">
<td><</td>
<td>Less than</td>
</tr>
<tr class="odd">
<td>>=</td>
<td>greater than or equal</td>
</tr>
<tr class="even">
<td><=</td>
<td>Less than or equal</td>
</tr>
</tbody>
</table></div>
<p></p>
<div class="sourceCode" id="cb16"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fl">3</span> <span class="op">></span> <span class="fl">5</span></span>
<span><span class="fl">5</span> <span class="op">==</span> <span class="fl">4</span></span>
<span><span class="fl">4</span> <span class="op">!=</span> <span class="fl">6</span></span>
<span><span class="va">a</span> <span class="op"><-</span> <span class="st">"big"</span></span>
<span><span class="va">b</span> <span class="op"><-</span> <span class="st">"small"</span></span>
<span><span class="va">a</span> <span class="op">==</span> <span class="va">b</span></span></code></pre></div>
<p></p>
</div>
<div id="logical-operators" class="section level3" number="3.1.4">
<h3>
<span class="header-section-number">3.1.4</span> Logical Operators<a class="anchor" aria-label="anchor" href="#logical-operators"><i class="fas fa-link"></i></a>
</h3>
<p>It is applicable only to vectors of type logical, numeric or complex. All numbers greater than 1 are considered as logical value TRUE.</p>
<p>Each element of the first vector is compared with the corresponding element of the second vector. The result of comparison is a Boolean value.</p>
<p><em>Logical operators are used to combine conditional statements:</em></p>
<div class="inline-table"><table class="table table-sm">
<colgroup>
<col width="25%">
<col width="31%">
<col width="43%">
</colgroup>
<thead><tr class="header">
<th align="left">Operator</th>
<th>Name</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="left">&</td>
<td>Element-wise Logical AND operator.</td>
<td align="left">It returns TRUE if both elements are TRUE</td>
</tr>
<tr class="even">
<td align="left">&&</td>
<td>Logical AND operator</td>
<td align="left">Returns TRUE if both statements are TRUE</td>
</tr>
<tr class="odd">
<td align="left">|</td>
<td>Elementwise- Logical OR operator.</td>
<td align="left">It returns TRUE if one of the statement is TRUE</td>
</tr>
<tr class="even">
<td align="left">||</td>
<td>Logical OR operator.</td>
<td align="left">It returns TRUE if one of the statement is TRUE.</td>
</tr>
<tr class="odd">
<td align="left">!</td>
<td>Logical NOT</td>
<td align="left">Returns FALSE if statement is TRUE</td>
</tr>
</tbody>
</table></div>
</div>
<div id="miscellaneous-operators" class="section level3" number="3.1.5">
<h3>
<span class="header-section-number">3.1.5</span> Miscellaneous Operators<a class="anchor" aria-label="anchor" href="#miscellaneous-operators"><i class="fas fa-link"></i></a>
</h3>
<div class="inline-table"><table class="table table-sm">
<thead><tr class="header">
<th align="left">Operator</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr class="odd">
<td align="left">:</td>
<td align="left">Creates a series of numbers in a sequence</td>
</tr>
<tr class="even">
<td align="left">%in%</td>
<td align="left">Find out if an element belongs to a vector</td>
</tr>
<tr class="odd">
<td align="left">%*%</td>
<td align="left">Matrix Multiplication</td>
</tr>
</tbody>
</table></div>
<p></p>
<div class="sourceCode" id="cb17"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">10</span></span>
<span><span class="va">x</span> <span class="op"><a href="https://rdrr.io/r/base/match.html">%in%</a></span> <span class="va">y</span></span>
<span><span class="va">x</span> <span class="op"><-</span> <span class="va">Matrix1</span> <span class="op"><a href="https://rdrr.io/r/base/matmult.html">%*%</a></span> <span class="va">Matrix2</span></span></code></pre></div>
<p></p>
</div>
</div>
<div id="data-objects" class="section level2" number="3.2">
<h2>
<span class="header-section-number">3.2</span> Data Objects<a class="anchor" aria-label="anchor" href="#data-objects"><i class="fas fa-link"></i></a>
</h2>
<p>At almost everything you will do in R is the concept that everything in R is an <a href="https://journal.r-project.org/archive/2009-1/RJournal_2009-1_Chambers.pdf">object</a>.</p>
<p>Generally, while doing programming, we need to use variables to store various information. Variables are reserved memory locations to store values i.e when we create a variable we allocate some memory space. In R the variable is an object. An object is a data structure having few attributes and methods which are applied to its attributes.</p>
<div id="data-types" class="section level3" number="3.2.1">
<h3>
<span class="header-section-number">3.2.1</span> Data Types<a class="anchor" aria-label="anchor" href="#data-types"><i class="fas fa-link"></i></a>
</h3>
<p><strong>Character</strong></p>
<p>A character object is represented by a collection of Alphabets,numbers and some symbols between double quotes (” “).</p>
<p></p>
<div class="sourceCode" id="cb18"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">my_name</span> <span class="op"><-</span> <span class="st">"Africano"</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/character.html">as.character</a></span><span class="op">(</span><span class="op">)</span> <span class="co"># convert to character</span></span></code></pre></div>
<p></p>
<p><strong>Numeric</strong></p>
<p>Numeric are used to represent continuous variables. E.g. are Time taken by all the athletes for completing a single lap in a race.</p>
<p></p>
<div class="sourceCode" id="cb19"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">a</span> <span class="op"><-</span> <span class="fl">20.4</span></span>
<span><span class="va">a</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op">(</span><span class="fl">20</span><span class="op">)</span> <span class="co"># convert to numeric</span></span></code></pre></div>
<p></p>
<p><strong>Integer</strong></p>
<p>Integers are natural numbers. They can be used to represent counting variables</p>
<p></p>
<div class="sourceCode" id="cb20"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">a</span> <span class="op"><-</span> <span class="fl">20</span></span>
<span><span class="va">a</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/integer.html">as.integer</a></span><span class="op">(</span><span class="fl">20.345</span><span class="op">)</span> <span class="co"># convert to integer</span></span></code></pre></div>
<p></p>
<p><strong>Complex</strong></p>
<p>To represent complex numbers object of type complex is used. For creating object of type complex use <strong><code><a href="https://rdrr.io/r/base/complex.html">as.complex()</a></code></strong> or <strong><code>complex</code></strong> functions.</p>
<p></p>
<div class="sourceCode" id="cb21"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/complex.html">as.complex</a></span><span class="op">(</span><span class="fl">35</span> <span class="op">+</span> <span class="op">(</span><span class="fl">0</span><span class="op">+</span><span class="fl">7i</span><span class="op">)</span><span class="op">)</span> <span class="co"># convert to complex number</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/complex.html">complex</a></span><span class="op">(</span>real <span class="op">=</span> <span class="fl">2</span>, im <span class="op">=</span> <span class="fl">1</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p><strong>Logical</strong></p>
<p>An object of data type logical can have the value TRUE or FALSE and is used to indicate if a condition is true or false.</p>
<p></p>
<div class="sourceCode" id="cb22"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/logical.html">as.logical</a></span><span class="op">(</span><span class="op">)</span> <span class="co"># convert to logical</span></span></code></pre></div>
<p></p>
</div>
<div id="data-structures" class="section level3" number="3.2.2">
<h3>
<span class="header-section-number">3.2.2</span> Data Structures<a class="anchor" aria-label="anchor" href="#data-structures"><i class="fas fa-link"></i></a>
</h3>
<p><strong>Vectors</strong></p>
<p>Used to store objects of the same type (think of them as arrays) Vector can be constructed using <strong><code><a href="https://rdrr.io/r/base/c.html">c()</a></code></strong></p>
<p></p>
<div class="sourceCode" id="cb23"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/vector.html">vector</a></span><span class="op">(</span><span class="op">)</span> <span class="co">#declares an empty vector</span></span>
<span><span class="va">a</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="fl">11</span>, <span class="fl">25</span>, <span class="fl">15.43</span>, <span class="op">-</span><span class="fl">0.6</span>, <span class="op">-</span><span class="fl">2</span>, <span class="fl">4</span><span class="op">)</span> <span class="co">#numeric vector</span></span>
<span><span class="va">b</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="cn">TRUE</span>, <span class="cn">T</span>, <span class="cn">FALSE</span>, <span class="cn">F</span><span class="op">)</span> <span class="co"># logical vector</span></span>
<span><span class="va">c</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"name"</span>, <span class="st">"x"</span>, <span class="st">"y"</span><span class="op">)</span> <span class="co">#character vector</span></span>
<span><span class="va">a</span><span class="op">[</span><span class="fl">1</span><span class="op">]</span> <span class="co">#accessing the first element in vector a</span></span>
<span><span class="va">b</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">4</span><span class="op">]</span> <span class="co">#accessing elements from 1st to 4th</span></span></code></pre></div>
<p></p>
<p><strong>Lists</strong></p>
<p>A list is like a vector.</p>
<p>However, an element of a list can be an object of any type and structure. Consequently, a list can contain another list and therefore it can be used to construct arbitrary data structures.<br></p>
<p>A list can also be constructed by using the function <strong><code><a href="https://rdrr.io/r/base/list.html">list()</a></code></strong></p>
<p></p>
<div class="sourceCode" id="cb24"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">id</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">4</span></span>
<span><span class="va">month</span> <span class="op"><-</span> <span class="va">month.name</span></span>
<span><span class="va">r</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/seq.html">seq</a></span><span class="op">(</span><span class="fl">2</span>, <span class="fl">3</span>, <span class="fl">0.1</span><span class="op">)</span></span>
<span><span class="va">my_list</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html">list</a></span><span class="op">(</span><span class="va">id</span>, <span class="va">month</span>, <span class="va">r</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/print.html">print</a></span><span class="op">(</span><span class="va">my_list</span><span class="op">)</span></span>
<span><span class="co"># accessing elements in a list</span></span>
<span><span class="va">my_list</span><span class="op">[[</span><span class="fl">1</span><span class="op">]</span><span class="op">]</span></span>
<span><span class="va">my_list</span><span class="op">[[</span><span class="fl">2</span><span class="op">]</span><span class="op">]</span></span></code></pre></div>
<p></p>
<p><strong>Factors</strong></p>
<p>The factor data type is used to represent categorical data.</p>
<p>In factor variables mostly few possible values will be seen. These values are called LEVELS.</p>
<p>For example: variable ‘Gender’ with values male and female.<br></p>
<p>If a variable is having just two levels it is called as Dichotomous.</p>
<p></p>
<div class="sourceCode" id="cb25"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">gender</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"male"</span>, <span class="st">"male"</span>, <span class="st">"female"</span>, <span class="st">"male"</span>, <span class="st">"female"</span><span class="op">)</span></span>
<span><span class="va">gender</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">as.factor</a></span><span class="op">(</span><span class="va">gender</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/levels.html">levels</a></span><span class="op">(</span><span class="va">Gender</span><span class="op">)</span> <span class="co"># We can look at levels</span></span>
<span><span class="va">satisfaction</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/factor.html">as.factor</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Extremely poor"</span>, <span class="st">"bad"</span>, <span class="st">"moderate"</span>,</span>
<span> <span class="st">"good"</span>, <span class="st">"Extremely good"</span>, <span class="st">"Extremely poor"</span>, <span class="st">"good"</span>, <span class="st">"moderate"</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/levels.html">levels</a></span><span class="op">(</span><span class="va">satisfaction</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p><strong>Matrices</strong> A matrix can be regarded as a generalization of a vector. All the elements of a matrix must be of the same data type.</p>
<p>i.e. Is a multi dimensional vector, they follow all the matrix algebraic operators and properties</p>
<p></p>
<div class="sourceCode" id="cb26"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">mat1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/matrix.html">matrix</a></span><span class="op">(</span>nrow <span class="op">=</span> <span class="fl">3</span>, ncol <span class="op">=</span> <span class="fl">3</span><span class="op">)</span> <span class="co"># creating a 3 x 3 empty matrix</span></span></code></pre></div>
<p></p>
<p><strong>Data Frames</strong> Data frames can also be regarded as an extension to matrices.</p>
<p>Data frames can have columns of different data types and are the most convenient data structure for data analysis in R. In fact, most statistical modeling routines in R require a data frame as input.</p>
<p>Data frame can be created using <strong><code><a href="https://rdrr.io/r/base/data.frame.html">data.frame()</a></code></strong></p>
<p></p>
<div class="sourceCode" id="cb27"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">Skill</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"R"</span>, <span class="st">"SQL"</span>, <span class="st">"Excel"</span><span class="op">)</span></span>
<span><span class="va">Expert</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html">c</a></span><span class="op">(</span><span class="st">"Africano"</span>, <span class="st">"Byamugisha"</span>, <span class="st">"Analyst 1"</span><span class="op">)</span></span>
<span><span class="va">df</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html">data.frame</a></span><span class="op">(</span><span class="va">Skill</span>, <span class="va">Expert</span><span class="op">)</span></span>
<span><span class="va">df</span></span></code></pre></div>
<p></p>
</div>
<div id="mixing-objects" class="section level3" number="3.2.3">
<h3>
<span class="header-section-number">3.2.3</span> <strong>Mixing Objects</strong><a class="anchor" aria-label="anchor" href="#mixing-objects"><i class="fas fa-link"></i></a>
</h3>
<p>This brings in coercion. Coercion happens in R when the type of objects are changed during computation either implicitly or by using functions for explicit coercion</p>
<p></p>
<div class="sourceCode" id="cb28"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="va">x</span> <span class="op"><-</span> <span class="fl">1</span><span class="op">:</span><span class="fl">3</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/typeof.html">typeof</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span></span>
<span><span class="va">x</span><span class="op">[</span><span class="fl">2</span><span class="op">]</span> <span class="op"><-</span> <span class="st">"hi"</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/typeof.html">typeof</a></span><span class="op">(</span><span class="va">x</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p>Notice that at first, <code>x</code> is of type <code>integer</code>. But when we assigned <code>x[2] = "hi"</code>, all the elements of <code>x</code> were coerced into <code>character</code> as vectors in R can only hold data of single type.</p>
<p><strong>Implicit Coercion</strong> Coercion happens with data types in R, often implicitly, so that the data can accommodate all the values, This is done by R with out the knowledge of the programmer.</p>
<p><strong>Explicit Coercion</strong> In explicit coercion , The use can change one data type to another data type by applying function by the programmer.</p>
<p></p>
<div class="sourceCode" id="cb29"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Examples of conversions</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/integer.html">as.integer</a></span><span class="op">(</span><span class="op">)</span> <span class="co"># Converts the object to integer type</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/character.html">as.character</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/factor.html">as.factor</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/as.data.frame.html">as.data.frame</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/numeric.html">as.numeric</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="object-attributes" class="section level3" number="3.2.4">
<h3>
<span class="header-section-number">3.2.4</span> Object Attributes<a class="anchor" aria-label="anchor" href="#object-attributes"><i class="fas fa-link"></i></a>
</h3>
<p>These are metadata that describes the data object</p>
<ul>
<li><p>names, dimnames</p></li>
<li><p>dimensions (e.g. matrices, arrays)</p></li>
<li><p>class (e.g. integer, numeric)</p></li>
<li><p>length</p></li>
</ul>
<p></p>
<div class="sourceCode" id="cb30"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/class.html">class</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/dimnames.html">dimnames</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/names.html">names</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/length.html">length</a></span><span class="op">(</span><span class="op">)</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/typeof.html">typeof</a></span><span class="op">(</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
</div>
<div id="tasks-1" class="section level2" number="3.3">
<h2>
<span class="header-section-number">3.3</span> Tasks<a class="anchor" aria-label="anchor" href="#tasks-1"><i class="fas fa-link"></i></a>
</h2>
<ol style="list-style-type: decimal">
<li>Can you spot the difference between a character string and a number? Here’s a test: Which of these are character strings and which are numbers? <code>1</code>, <code>"1"</code>, <code>"one"</code>.</li>
<li>Create an atomic vector of Character type, double type, logical type , integer type, complex type and raw type</li>
<li>Check whether each of the created data structure is of vector type as well as check the class of each of the data.</li>
<li>Create a List of heterogeneous data, which include numeric, character and logical vectors and print the lists.</li>
<li>Create a matrix of 3 rows and 4 columns which stores data from 1 to 12 and arrange the value row wise.</li>
<li>Create a Data Frame of 5 employees .</li>
</ol>
</div>
</div>
<div class="chapter-nav">
<div class="prev"><a href="getting-started-setting-up.html"><span class="header-section-number">2</span> Getting Started & Setting Up</a></div>
<div class="next"><a href="working-with-data.html"><span class="header-section-number">4</span> Working With Data</a></div>
</div></main><div class="col-md-3 col-lg-2 d-none d-md-block sidebar sidebar-chapter">
<nav id="toc" data-toggle="toc" aria-label="On this page"><h2>On this page</h2>
<ul class="nav navbar-nav">
<li><a class="nav-link" href="#operators-data-objects"><span class="header-section-number">3</span> Operators & Data Objects</a></li>
<li>
<a class="nav-link" href="#operators"><span class="header-section-number">3.1</span> Operators</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#assignment-operators"><span class="header-section-number">3.1.1</span> Assignment Operators</a></li>
<li><a class="nav-link" href="#arithmetic-operators"><span class="header-section-number">3.1.2</span> Arithmetic Operators</a></li>
<li><a class="nav-link" href="#relational-operators"><span class="header-section-number">3.1.3</span> Relational Operators</a></li>
<li><a class="nav-link" href="#logical-operators"><span class="header-section-number">3.1.4</span> Logical Operators</a></li>
<li><a class="nav-link" href="#miscellaneous-operators"><span class="header-section-number">3.1.5</span> Miscellaneous Operators</a></li>
</ul>
</li>
<li>
<a class="nav-link" href="#data-objects"><span class="header-section-number">3.2</span> Data Objects</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#data-types"><span class="header-section-number">3.2.1</span> Data Types</a></li>
<li><a class="nav-link" href="#data-structures"><span class="header-section-number">3.2.2</span> Data Structures</a></li>
<li><a class="nav-link" href="#mixing-objects"><span class="header-section-number">3.2.3</span> Mixing Objects</a></li>
<li><a class="nav-link" href="#object-attributes"><span class="header-section-number">3.2.4</span> Object Attributes</a></li>
</ul>
</li>
<li><a class="nav-link" href="#tasks-1"><span class="header-section-number">3.3</span> Tasks</a></li>
</ul>
<div class="book-extra">
<ul class="list-unstyled">
</ul>
</div>
</nav>
</div>
</div>
</div> <!-- .container -->
<footer class="bg-primary text-light mt-5"><div class="container"><div class="row">
<div class="col-12 col-md-6 mt-3">
<p>"<strong>A Comprehensive Guide to Use R</strong>" was written by Africano BYAMUGISHA. It was last built on 2023-01-05.</p>
</div>
<div class="col-12 col-md-6 mt-3">
<p>This book was built by the <a class="text-light" href="https://bookdown.org">bookdown</a> R package.</p>
</div>
</div></div>
</footer>
</body>
</html>