-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworking-with-data.html
493 lines (479 loc) · 37.7 KB
/
working-with-data.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
<!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>4 Working With Data | A Comprehensive Guide to Use R</title>
<meta name="author" content="Africano BYAMUGISHA">
<meta name="description" content="You can download the demo data files fro various types from the link below:- CLICK here to open the datasets folder 4.1 Importing Data into R 4.1.1 Import flat files (.csv, .txt ) read.table()...">
<meta name="generator" content="bookdown 0.33 with bs4_book()">
<meta property="og:title" content="4 Working With Data | A Comprehensive Guide to Use R">
<meta property="og:type" content="book">
<meta property="og:url" content="https://github.io/africanobyamugisha/working-with-data.html">
<meta property="og:image" content="https://github.io/africanobyamugisha/images/flyer.png">
<meta property="og:description" content="You can download the demo data files fro various types from the link below:- CLICK here to open the datasets folder 4.1 Importing Data into R 4.1.1 Import flat files (.csv, .txt ) read.table()...">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="4 Working With Data | A Comprehensive Guide to Use R">
<meta name="twitter:description" content="You can download the demo data files fro various types from the link below:- CLICK here to open the datasets folder 4.1 Importing Data into R 4.1.1 Import flat files (.csv, .txt ) read.table()...">
<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="" href="operators-data-objects.html"><span class="header-section-number">3</span> Operators & Data Objects</a></li>
<li><a class="active" 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="working-with-data" class="section level1" number="4">
<h1>
<span class="header-section-number">4</span> Working With Data<a class="anchor" aria-label="anchor" href="#working-with-data"><i class="fas fa-link"></i></a>
</h1>
<div class="inline-figure"><img src="images/working_with_data.jfif" style="width:50.0%"></div>
<p>You can download the demo data files fro various types from the link below:-</p>
<p><a href="https://github.com/africanobyamugisha/datasets/tree/main/guide_to_R_programming%20data" title="Open data set folder">CLICK here to open the datasets folder</a></p>
<div id="importing-data-into-r" class="section level2" number="4.1">
<h2>
<span class="header-section-number">4.1</span> Importing Data into R<a class="anchor" aria-label="anchor" href="#importing-data-into-r"><i class="fas fa-link"></i></a>
</h2>
<div class="inline-figure"><img src="images/data_import.png" style="width:70.0%"></div>
<div id="import-flat-files-.csv-.txt" class="section level3" number="4.1.1">
<h3>
<span class="header-section-number">4.1.1</span> Import flat files (<code>.csv</code>, <code>.txt</code> )<a class="anchor" aria-label="anchor" href="#import-flat-files-.csv-.txt"><i class="fas fa-link"></i></a>
</h3>
<ul>
<li><p><code><a href="https://rdrr.io/r/utils/read.table.html">read.table()</a></code> is the main function</p></li>
<li><p><code><a href="https://rdrr.io/r/utils/read.table.html">read.csv()</a></code> wrapper for CSV</p></li>
<li><p><code><a href="https://rdrr.io/r/utils/read.table.html">read.delim()</a></code> wrapper for tab-delimited files</p></li>
</ul>
<div class="inline-figure"><img src="images/csv_wrappers.png"></div>
<p></p>
<div class="sourceCode" id="cb31"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># install th read r package</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op">(</span><span class="va">readr</span><span class="op">)</span></span>
<span><span class="co"># load the read r package</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://readr.tidyverse.org">readr</a></span><span class="op">)</span></span>
<span><span class="co"># read csv file using read.table()</span></span>
<span><span class="va">df1</span> <span class="op"><-</span> <span class="fu"><a href="https://readr.tidyverse.org/reference/read_delim.html">read_csv</a></span><span class="op">(</span><span class="st">"data/demo csv data.csv"</span>, show_col_types <span class="op">=</span> <span class="cn">FALSE</span><span class="op">)</span></span>
<span><span class="co"># read txt file that doesnot headers</span></span>
<span><span class="va">df2</span> <span class="op"><-</span> <span class="fu"><a href="https://readr.tidyverse.org/reference/read_delim.html">read_delim</a></span><span class="op">(</span><span class="st">"data/demo data text.txt"</span><span class="op">)</span></span>
<span><span class="co"># Or use</span></span>
<span><span class="va">df3</span> <span class="op"><-</span> <span class="fu"><a href="https://readr.tidyverse.org/reference/read_delim.html">read_tsv</a></span><span class="op">(</span><span class="st">"data/demo data text.txt"</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="import-excel-data-file" class="section level3" number="4.1.2">
<h3>
<span class="header-section-number">4.1.2</span> Import Excel data file<a class="anchor" aria-label="anchor" href="#import-excel-data-file"><i class="fas fa-link"></i></a>
</h3>
<p></p>
<div class="sourceCode" id="cb32"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># install package</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op">(</span><span class="st">"readxl"</span><span class="op">)</span></span>
<span><span class="co"># load package</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://readxl.tidyverse.org">readxl</a></span><span class="op">)</span></span>
<span><span class="co"># by default, it loads the first sheet</span></span>
<span><span class="va">excel_data</span> <span class="op"><-</span> <span class="fu"><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel</a></span><span class="op">(</span><span class="st">"data/demo excel data.xlsx"</span><span class="op">)</span></span>
<span><span class="co"># if the excell workbook has many sheets and you want to</span></span>
<span><span class="co"># load a specific one, e,g, sheet two</span></span>
<span><span class="va">excel_data</span> <span class="op"><-</span> <span class="fu"><a href="https://readxl.tidyverse.org/reference/read_excel.html">read_excel</a></span><span class="op">(</span><span class="st">"data/demo excel data.xlsx"</span>, sheet <span class="op">=</span> <span class="fl">2</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="import-stata-data-file" class="section level3" number="4.1.3">
<h3>
<span class="header-section-number">4.1.3</span> Import STATA data file<a class="anchor" aria-label="anchor" href="#import-stata-data-file"><i class="fas fa-link"></i></a>
</h3>
<p></p>
<div class="sourceCode" id="cb33"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op">(</span><span class="st">"readstata13"</span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://github.com/sjewo/readstata13">readstata13</a></span><span class="op">)</span></span>
<span><span class="va">stata_data</span> <span class="op"><-</span> <span class="fu">read.dta13</span><span class="op">(</span><span class="st">"data/demo stata data.dta"</span>, generate.factors <span class="op">=</span> <span class="cn">TRUE</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="importing-r-data-file" class="section level3" number="4.1.4">
<h3>
<span class="header-section-number">4.1.4</span> Importing R data file<a class="anchor" aria-label="anchor" href="#importing-r-data-file"><i class="fas fa-link"></i></a>
</h3>
<p></p>
<div class="sourceCode" id="cb34"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/load.html">load</a></span><span class="op">(</span><span class="st">"data/demo R data.rda"</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="importing-web-data-file" class="section level3" number="4.1.5">
<h3>
<span class="header-section-number">4.1.5</span> Importing Web data file<a class="anchor" aria-label="anchor" href="#importing-web-data-file"><i class="fas fa-link"></i></a>
</h3>
<p>Data on the web comes in several modes, for example:</p>
<ol style="list-style-type: decimal">
<li><p>files that you can download</p></li>
<li><p>APIs</p></li>
<li><p>content such as HTML tables</p></li>
<li><p>custom data browsers</p></li>
<li><p>and more.</p></li>
</ol>
<p>However, for this section, let us keep it basic, If interested is the other option, contact the <a href="https://africanobyamugisha.github.io/guide_to_R_pragramming/about-the-author.html">author</a> for some materials OR code that might not be in this book.</p>
</div>
<div id="inbuilt-datasets" class="section level3" number="4.1.6">
<h3>
<span class="header-section-number">4.1.6</span> Inbuilt Datasets<a class="anchor" aria-label="anchor" href="#inbuilt-datasets"><i class="fas fa-link"></i></a>
</h3>
<p>To see the list of pre-loaded datasets, type the function <strong><code><a href="https://rdrr.io/r/utils/data.html">data()</a></code></strong></p>
<p></p>
<div class="sourceCode" id="cb35"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Loading</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">)</span></span>
<span><span class="co"># learn more about the data</span></span>
<span><span class="fu">`?`</span><span class="op">(</span><span class="va">mtcars</span><span class="op">)</span></span>
<span><span class="co"># OR</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/help.html">help</a></span><span class="op">(</span><span class="va">mtcars</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p>In case you want to get a list of datasets in a specific R package using <code><a href="https://rdrr.io/r/utils/data.html">data()</a></code></p>
<p></p>
<div class="sourceCode" id="cb36"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># list available datasets in ggplot2 package</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/data.html">data</a></span><span class="op">(</span>package <span class="op">=</span> <span class="st">"ggplot2"</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="other-file-formats" class="section level3" number="4.1.7">
<h3>
<span class="header-section-number">4.1.7</span> Other file formats<a class="anchor" aria-label="anchor" href="#other-file-formats"><i class="fas fa-link"></i></a>
</h3>
<div class="inline-table"><table style="width:98%;" class="table table-sm">
<colgroup>
<col width="16%">
<col width="13%">
<col width="14%">
<col width="54%">
</colgroup>
<thead><tr class="header">
<th>File Type</th>
<th>File extension</th>
<th>function</th>
<th>package</th>
</tr></thead>
<tbody>
<tr class="odd">
<td>Database file type</td>
<td>.dbf</td>
<td>read.dbf()</td>
<td>foreign</td>
</tr>
<tr class="even">
<td>SPSS data file</td>
<td>.sav</td>
<td>read_spss()</td>
<td>haven</td>
</tr>
<tr class="odd">
<td>SQL Database</td>
<td></td>
<td><ul>
<li><p>dbConnect()</p></li>
<li><p>dbGetQuery()</p></li>
</ul></td>
<td>DBI, RMySQL, RPostgreSQL, ROracle depending on the specific database</td>
</tr>
<tr class="even">
<td>SAS data file</td>
<td>.xport</td>
<td>read_sas()</td>
<td>haven</td>
</tr>
<tr class="odd">
<td>Matlab file</td>
<td></td>
<td>readMat()</td>
<td>R.matlap</td>
</tr>
</tbody>
</table></div>
</div>
</div>
<div id="explore-the-dataset" class="section level2" number="4.2">
<h2>
<span class="header-section-number">4.2</span> Explore the dataset<a class="anchor" aria-label="anchor" href="#explore-the-dataset"><i class="fas fa-link"></i></a>
</h2>
<p>In R,There are various functions to explore a data frame. These functions can help you get a better understanding of the structure and contents of your data frame.</p>
<p>Here are some common functions.</p>
<ol style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/utils/head.html">head()</a></code></strong> - shows the first few rows of the data frame</li>
</ol>
<!-- --><ol start="2" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/utils/head.html">tail()</a></code></strong> - shows the last few rows of the data frame</li>
</ol>
<!-- --><ol start="3" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/utils/str.html">str()</a></code></strong> - displays the structure of the data frame</li>
</ol>
<!-- --><ol start="4" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/summary.html">summary()</a></code></strong> - provides summary statistics for each variable in the data frame</li>
</ol>
<!-- --><ol start="5" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/nrow.html">nrow()</a></code></strong> - returns the number of rows in the data frame</li>
</ol>
<!-- --><ol start="6" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/nrow.html">ncol()</a></code></strong> - returns the number of columns in the data frame</li>
</ol>
<!-- --><ol start="7" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/colnames.html">colnames()</a></code></strong> - returns the names of the columns in the data frame</li>
</ol>
<!-- --><ol start="8" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/row.names.html">row.names()</a></code></strong> - returns the names of the rows in the data frame</li>
</ol>
<!-- --><ol start="9" style="list-style-type: decimal">
<li><p><strong><code><a href="https://rdrr.io/r/base/unique.html">unique()</a></code></strong> - returns unique values in a column of the data frame</p></li>
<li><p><strong><code><a href="https://tibble.tidyverse.org/reference/view.html">view()</a></code></strong> - shows the dataset in spreadsheet form</p></li>
<li><p><strong><code><a href="https://rdrr.io/r/base/dim.html">dim()</a></code></strong> - returns the dimensions of the data frame as a vector of two integers, the number of rows and the number of columns</p></li>
<li><p><strong><code><a href="https://rdrr.io/r/base/NA.html">is.na()</a></code></strong> - returns a logical vector indicating whether each value in the data frame is missing or not</p></li>
</ol>
<p>For a better visual display of the results, additional packages can be used such as s<code>kimr, gtsummary, kable</code>, as it will be in the preceding sections of this guide, however, below is a sneak peak of the basic ones.</p>
<p></p>
<div class="sourceCode" id="cb37"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/utils/install.packages.html">install.packages</a></span><span class="op">(</span><span class="st">"skimr"</span><span class="op">)</span></span>
<span><span class="kw"><a href="https://rdrr.io/r/base/library.html">library</a></span><span class="op">(</span><span class="va"><a href="https://docs.ropensci.org/skimr/">skimr</a></span><span class="op">)</span></span>
<span><span class="co"># get the summary of all the variable, alternative to</span></span>
<span><span class="co"># ---str()---</span></span>
<span><span class="fu"><a href="https://docs.ropensci.org/skimr/reference/skim.html">skim</a></span><span class="op">(</span><span class="va">iris</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
<div id="pull-basic-statistics-from-your-data" class="section level2" number="4.3">
<h2>
<span class="header-section-number">4.3</span> Pull basic statistics from your data<a class="anchor" aria-label="anchor" href="#pull-basic-statistics-from-your-data"><i class="fas fa-link"></i></a>
</h2>
<div id="for-continuous-variables" class="section level3" number="4.3.1">
<h3>
<span class="header-section-number">4.3.1</span> <strong>For continuous variables:</strong><a class="anchor" aria-label="anchor" href="#for-continuous-variables"><i class="fas fa-link"></i></a>
</h3>
<ol style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/mean.html">mean()</a></code></strong> - calculates the mean of a numeric vector</li>
</ol>
<p></p>
<div class="sourceCode" id="cb38"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/mean.html">mean</a></span><span class="op">(</span><span class="va">numeric_vector</span><span class="op">)</span></span></code></pre></div>
<p></p>
<ol start="2" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/stats/median.html">median()</a></code></strong> - calculates the median of a numeric vector</li>
</ol>
<p></p>
<div class="sourceCode" id="cb39"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/stats/median.html">median</a></span><span class="op">(</span><span class="va">numeric_vector</span><span class="op">)</span></span></code></pre></div>
<p></p>
<ol start="3" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/stats/sd.html">sd()</a></code></strong> - calculates the standard deviation of a numeric vector</li>
</ol>
<p></p>
<div class="sourceCode" id="cb40"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/stats/sd.html">sd</a></span><span class="op">(</span><span class="va">numeric_vector</span><span class="op">)</span></span></code></pre></div>
<p></p>
<ol start="4" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/stats/cor.html">var()</a></code></strong> - calculates the variance of a numeric vector</li>
</ol>
<!-- --><ol start="5" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/Extremes.html">min()</a></code></strong> - returns the minimum value of a numeric vector</li>
</ol>
<!-- --><ol start="6" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/Extremes.html">max()</a></code></strong> - returns the maximum value of a numeric vector</li>
</ol>
<!-- --><ol start="7" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/range.html">range()</a></code></strong> - returns the range of a numeric vector as a vector of length 2 containing the minimum and maximum values</li>
</ol>
<!-- --><ol start="8" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/stats/quantile.html">quantile()</a></code></strong> - calculates the quantiles of a numeric vector</li>
</ol>
<!-- --><ol start="9" style="list-style-type: decimal">
<li><p><strong><code><a href="https://rdrr.io/r/base/summary.html">summary()</a></code></strong> - provides summary statistics for a numeric vector, including the minimum, 1st quartile, median, mean, 3rd quartile, and maximum</p></li>
<li><p>Simple plots using the base package</p></li>
</ol>
<p></p>
<div class="sourceCode" id="cb41"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Histogram:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/hist.html">hist</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name</span><span class="op">)</span></span>
<span><span class="co"># Boxplot:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/boxplot.html">boxplot</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name</span><span class="op">)</span></span>
<span><span class="co"># Density Plot:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/stats/density.html">density</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="co"># Scatterplot:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/plot.default.html">plot</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name1</span>, <span class="va">data</span><span class="op">$</span><span class="va">variable_name2</span><span class="op">)</span></span></code></pre></div>
<p></p>
<p><strong>For categorical/character variables:</strong></p>
<ol style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/table.html">table()</a></code></strong> - creates a contingency table of the frequencies of the levels of a categorical variable</li>
</ol>
<!-- --><ol start="2" style="list-style-type: decimal">
<li>
<strong><code><a href="https://rdrr.io/r/base/proportions.html">prop.table()</a></code></strong> - converts a contingency table of frequencies to a contingency table of proportions</li>
</ol>
<!-- --><ol start="3" style="list-style-type: decimal">
<li><p><strong><code><a href="https://rdrr.io/r/base/summary.html">summary()</a></code></strong> - provides a summary of a factor variable, including the number of levels and the frequency of the most common level.</p></li>
<li><p>Simple plot using the base package</p></li>
</ol>
<p></p>
<div class="sourceCode" id="cb42"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="co"># Bar Plot:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/barplot.html">barplot</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/table.html">table</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="co"># Pie Chart:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/pie.html">pie</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/table.html">table</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name</span><span class="op">)</span><span class="op">)</span></span>
<span><span class="co"># Stacked Bar Plot:</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/graphics/barplot.html">barplot</a></span><span class="op">(</span><span class="fu"><a href="https://rdrr.io/r/base/table.html">table</a></span><span class="op">(</span><span class="va">data</span><span class="op">$</span><span class="va">variable_name</span>, <span class="va">data</span><span class="op">$</span><span class="va">grouping_variable</span><span class="op">)</span>, col <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">"red"</span>,</span>
<span> <span class="st">"blue"</span><span class="op">)</span><span class="op">)</span></span></code></pre></div>
<p></p>
</div>
</div>
<div id="exporting-data-from-r" class="section level2" number="4.4">
<h2>
<span class="header-section-number">4.4</span> Exporting Data from R<a class="anchor" aria-label="anchor" href="#exporting-data-from-r"><i class="fas fa-link"></i></a>
</h2>
<p></p>
<div class="sourceCode" id="cb43"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="fu"><a href="https://rdrr.io/r/base/save.html">save</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.rda"</span><span class="op">)</span> <span class="co"># exporting R data object</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/write.table.html">write.csv</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.csv"</span><span class="op">)</span> <span class="co"># exporting csv data file</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/utils/write.table.html">write.table</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.txt"</span><span class="op">)</span> <span class="co"># exporting text data file</span></span>
<span><span class="fu">writexl</span><span class="fu">::</span><span class="fu">write_xlsx</span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.rda"</span><span class="op">)</span> <span class="co"># exporting excel data file</span></span>
<span><span class="fu">jsonlite</span><span class="fu">::</span><span class="fu"><a href="https://rdrr.io/pkg/jsonlite/man/read_json.html">write_json</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.json"</span><span class="op">)</span> <span class="co"># exporting java script data file</span></span>
<span><span class="fu">xml2</span><span class="fu">::</span><span class="fu"><a href="http://xml2.r-lib.org/reference/write_xml.html">write_xml</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.xml"</span><span class="op">)</span> <span class="co"># exporting to xml file</span></span>
<span><span class="fu">haven</span><span class="fu">::</span><span class="fu"><a href="https://haven.tidyverse.org/reference/write_sas.html">write_sas</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.xport"</span><span class="op">)</span> <span class="co"># exporting SAS data file</span></span>
<span><span class="fu">haven</span><span class="fu">::</span><span class="fu"><a href="https://haven.tidyverse.org/reference/read_dta.html">write_dta</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.dta"</span><span class="op">)</span> <span class="co"># exporting STATA data file</span></span>
<span><span class="fu">haven</span><span class="fu">::</span><span class="fu"><a href="https://haven.tidyverse.org/reference/read_spss.html">write_sav</a></span><span class="op">(</span><span class="va">data_object</span>, file <span class="op">=</span> <span class="st">"my_data.sav"</span><span class="op">)</span> <span class="co"># exporting SPSS data file</span></span></code></pre></div>
<p></p>
<p>You can download the demo data files fro various types from the link below:-</p>
<p><a href="https://github.com/africanobyamugisha/datasets/tree/main/guide_to_R_programming%20data" title="Open data set folder">CLICK here to open the datasets folder</a></p>
</div>
<div id="tasks-2" class="section level2" number="4.5">
<h2>
<span class="header-section-number">4.5</span> <strong>Tasks</strong><a class="anchor" aria-label="anchor" href="#tasks-2"><i class="fas fa-link"></i></a>
</h2>
<div id="exercise-1" class="section level3" number="4.5.1">
<h3>
<span class="header-section-number">4.5.1</span> Exercise 1<a class="anchor" aria-label="anchor" href="#exercise-1"><i class="fas fa-link"></i></a>
</h3>
<ol style="list-style-type: decimal">
<li>
<p>Import a CSV file named “sales_data.csv” into RStudio.</p>
<p><strong>NOTE:</strong> You can download the sales data to your computer from; <a href="https://github.com/africanobyamugisha/datasets/blob/main/guide_to_R_programming%20data/sales_data.csv" title="Click here Download the Sales Data">Click here Download the Sales Data</a></p>
</li>
<li><p>Export the data in the “sales_data” object as a CSV file named “sales_data_export.csv”.</p></li>
<li><p>Using the “summary” function, extract basic statistics (mean, median, minimum, maximum) for the “sales_data” object.</p></li>
<li><p>Load a new data file named “customer_data.xlsx” into RStudio.</p></li>
<li><p>Explore the data by using the “head” and “tail” functions to view the first and last few rows of the data.</p></li>
<li><p>Use the “str” function to display the structure of the “customer_data” object.</p></li>
<li><p>Pull basic statistics (mean, median, minimum, maximum) for the “customer_data” object using the “summary” function.</p></li>
<li><p>Export the data in the “customer_data” object as a CSV file named “customer_data_export.csv”.</p></li>
</ol>
</div>
<div id="exercise-2" class="section level3" number="4.5.2">
<h3>
<span class="header-section-number">4.5.2</span> Exercise 2<a class="anchor" aria-label="anchor" href="#exercise-2"><i class="fas fa-link"></i></a>
</h3>
<p>Import the inbuilt data of <code>iris</code> into RStudio.</p>
<ol style="list-style-type: decimal">
<li><p>Using the “plot” function, create a scatter plot of the “Sepal.Length” and “Sepal.Width” columns in the “iris” object.</p></li>
<li><p>Using the “boxplot” function, create a box plot of the “Petal.Length” column in the “iris” object, grouped by the “Species” column.</p></li>
<li><p>Using the “hist” function, create a histogram of the “Petal.Width” column in the “iris” object, grouped by the “Species” column.</p></li>
<li><p>Using the “plot” function, create a line plot of the “Sepal.Length” column in the “iris” object, grouped by the “Species” column.</p></li>
</ol>
</div>
</div>
</div>
<div class="chapter-nav">
<div class="prev"><a href="operators-data-objects.html"><span class="header-section-number">3</span> Operators & Data Objects</a></div>
<div class="next"><a href="data-wrangling.html"><span class="header-section-number">5</span> Data Wrangling</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="#working-with-data"><span class="header-section-number">4</span> Working With Data</a></li>
<li>
<a class="nav-link" href="#importing-data-into-r"><span class="header-section-number">4.1</span> Importing Data into R</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#import-flat-files-.csv-.txt"><span class="header-section-number">4.1.1</span> Import flat files (.csv, .txt )</a></li>
<li><a class="nav-link" href="#import-excel-data-file"><span class="header-section-number">4.1.2</span> Import Excel data file</a></li>
<li><a class="nav-link" href="#import-stata-data-file"><span class="header-section-number">4.1.3</span> Import STATA data file</a></li>
<li><a class="nav-link" href="#importing-r-data-file"><span class="header-section-number">4.1.4</span> Importing R data file</a></li>
<li><a class="nav-link" href="#importing-web-data-file"><span class="header-section-number">4.1.5</span> Importing Web data file</a></li>
<li><a class="nav-link" href="#inbuilt-datasets"><span class="header-section-number">4.1.6</span> Inbuilt Datasets</a></li>
<li><a class="nav-link" href="#other-file-formats"><span class="header-section-number">4.1.7</span> Other file formats</a></li>
</ul>
</li>
<li><a class="nav-link" href="#explore-the-dataset"><span class="header-section-number">4.2</span> Explore the dataset</a></li>
<li>
<a class="nav-link" href="#pull-basic-statistics-from-your-data"><span class="header-section-number">4.3</span> Pull basic statistics from your data</a><ul class="nav navbar-nav"><li><a class="nav-link" href="#for-continuous-variables"><span class="header-section-number">4.3.1</span> For continuous variables:</a></li></ul>
</li>
<li><a class="nav-link" href="#exporting-data-from-r"><span class="header-section-number">4.4</span> Exporting Data from R</a></li>
<li>
<a class="nav-link" href="#tasks-2"><span class="header-section-number">4.5</span> Tasks</a><ul class="nav navbar-nav">
<li><a class="nav-link" href="#exercise-1"><span class="header-section-number">4.5.1</span> Exercise 1</a></li>
<li><a class="nav-link" href="#exercise-2"><span class="header-section-number">4.5.2</span> Exercise 2</a></li>
</ul>
</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>