-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadditional_questions_solutions.html
670 lines (586 loc) · 34.8 KB
/
additional_questions_solutions.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="author" content="Benedict Anderer, Farhan Shaikh, and Saurav Jha" />
<meta name="date" content="2024-10-25" />
<title>String processing in R</title>
<script src="additional_questions_solutions_files/header-attrs-2.28/header-attrs.js"></script>
<script src="additional_questions_solutions_files/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="additional_questions_solutions_files/bootstrap-3.3.5/css/lumen.min.css" rel="stylesheet" />
<script src="additional_questions_solutions_files/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="additional_questions_solutions_files/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="additional_questions_solutions_files/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="additional_questions_solutions_files/jqueryui-1.13.2/jquery-ui.min.js"></script>
<link href="additional_questions_solutions_files/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="additional_questions_solutions_files/tocify-1.9.1/jquery.tocify.js"></script>
<script src="additional_questions_solutions_files/navigation-1.1/tabsets.js"></script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; 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
{ background-color: #f8f8f8; }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ef2929; } /* Alert */
code span.an { color: #8f5902; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #c4a000; } /* Attribute */
code span.bn { color: #0000cf; } /* BaseN */
code span.cf { color: #204a87; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4e9a06; } /* Char */
code span.cn { color: #000000; } /* Constant */
code span.co { color: #8f5902; font-style: italic; } /* Comment */
code span.cv { color: #8f5902; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #8f5902; font-weight: bold; font-style: italic; } /* Documentation */
code span.dt { color: #204a87; } /* DataType */
code span.dv { color: #0000cf; } /* DecVal */
code span.er { color: #a40000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #0000cf; } /* Float */
code span.fu { color: #000000; } /* Function */
code span.im { } /* Import */
code span.in { color: #8f5902; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #204a87; font-weight: bold; } /* Keyword */
code span.op { color: #ce5c00; font-weight: bold; } /* Operator */
code span.ot { color: #8f5902; } /* Other */
code span.pp { color: #8f5902; font-style: italic; } /* Preprocessor */
code span.sc { color: #000000; } /* SpecialChar */
code span.ss { color: #4e9a06; } /* SpecialString */
code span.st { color: #4e9a06; } /* String */
code span.va { color: #000000; } /* Variable */
code span.vs { color: #4e9a06; } /* VerbatimString */
code span.wa { color: #8f5902; font-weight: bold; font-style: italic; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<link rel="stylesheet" href="custom.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before, .tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "\e259";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "\e258";
font-family: 'Glyphicons Halflings';
border: none;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div id="header">
<h1 class="title toc-ignore">String processing in R</h1>
<h3 class="subtitle">The package <strong>stringi</strong></h3>
<h4 class="author">Benedict Anderer, Farhan Shaikh, and Saurav Jha</h4>
<h4 class="date">2024-10-25</h4>
</div>
<div id="welcome-to-our-workshop" class="section level1">
<h1>🎉 WELCOME TO OUR WORKSHOP ! 🎉</h1>
<p><strong>Exercise 2</strong>: In this exercise we will scrape Berlin’s
climate data from its wikipedia page and prepared it for analysis. We
will explore how <strong>stringi</strong> can be creatively applied for
<strong>scraping and cleaning numeric data</strong> by treating numbers
as strings—a powerful trick in data pre-processing. 🧹</p>
<hr />
<div id="lets-begin" class="section level3">
<h3>Let’s Begin! 🚀</h3>
</div>
<div id="exercise-2" class="section level3">
<h3>Exercise 2</h3>
<p><strong>Exercise 2.1 </strong>: <strong>Data Scraping</strong>
Extract all the table from this wikipedia page (<a
href="https://en.wikipedia.org/wiki/Berlin"
class="uri">https://en.wikipedia.org/wiki/Berlin</a>).</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>url<span class="ot"><-</span> <span class="st">"https://en.wikipedia.org/wiki/Berlin"</span> </span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>page <span class="ot"><-</span> <span class="fu">read_html</span>(url) </span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a>all_tables <span class="ot"><-</span> page <span class="sc">%>%</span></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">html_nodes</span>(<span class="st">"table"</span>) </span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Extract text from each table</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="co"># html_text() extracts only the textual content from the tables, removing all HTML tags and formatting. </span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a>text_tables <span class="ot"><-</span> <span class="fu">sapply</span>(all_tables, html_text)</span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a><span class="co"># Display the structure of the extracted text</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a><span class="co"># This results in a plain text representation, making it easier to read but lacking the original table structure. </span></span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(text_tables, <span class="at">nchar.max =</span> <span class="dv">65</span>, <span class="at">vec.len =</span> <span class="dv">5</span>, <span class="at">strict.width =</span> <span class="st">"wrap"</span>)</span></code></pre></div>
<pre><code>chr [1:63] "BerlinCapital city, state and municipality.mw-pa"| __truncated__
"Climate data for Berlin (Brandenburg), 1991–2020"| __truncated__ "Climate
data for Berlin (Dahlem), 58 m or 190 ft"| __truncated__ "Historical
populationYearPop.±%172165,300— 17"| __truncated__ "Residents by
Citizenship (31 December 2023)[103]"| __truncated__ ...</code></pre>
<p><strong>Exercise 2.2 </strong>: <strong>Data Detection</strong>
Detect climate data table and separate from all tables using
stringi.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Using stringi's string_detect function </span></span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Following line searches the text_tables vector for the presence of the phrase "climate data" (case-insensitive), returns the index of first occurence and assigns it to idx</span></span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a>idx <span class="ot"><-</span> <span class="fu">which</span>(<span class="fu">stri_detect_fixed</span>(text_tables, <span class="st">"climate data"</span>,</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a><span class="at">case_insensitive=</span><span class="cn">TRUE</span>, <span class="at">max_count=</span><span class="dv">1</span>))</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">as.data.frame</span>(<span class="fu">html_table</span>(all_tables[[idx]], <span class="at">fill=</span><span class="cn">TRUE</span>))</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(x) </span></code></pre></div>
<pre><code> Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Month
2 Record high °C (°F)
3 Mean maximum °C (°F)
4 Mean daily maximum °C (°F)
5 Daily mean °C (°F)
6 Mean daily minimum °C (°F)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Jan
2 15.1(59.2)
3 10.6(51.1)
4 3.2(37.8)
5 0.7(33.3)
6 −2.2(28.0)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Feb
2 19.2(66.6)
3 12.4(54.3)
4 4.9(40.8)
5 1.6(34.9)
6 −1.8(28.8)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Mar
2 25.8(78.4)
3 17.9(64.2)
4 9.0(48.2)
5 4.6(40.3)
6 0.4(32.7)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Apr
2 30.8(87.4)
3 24.0(75.2)
4 15.1(59.2)
5 9.7(49.5)
6 4.0(39.2)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 May
2 32.7(90.9)
3 28.4(83.1)
4 19.6(67.3)
5 14.2(57.6)
6 8.2(46.8)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Jun
2 38.4(101.1)
3 31.5(88.7)
4 22.9(73.2)
5 17.6(63.7)
6 11.7(53.1)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Jul
2 38.3(100.9)
3 32.7(90.9)
4 25.0(77.0)
5 19.6(67.3)
6 14.0(57.2)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Aug
2 38.0(100.4)
3 32.7(90.9)
4 24.8(76.6)
5 19.2(66.6)
6 13.5(56.3)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Sep
2 34.1(93.4)
3 26.9(80.4)
4 19.8(67.6)
5 14.7(58.5)
6 9.8(49.6)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Oct
2 27.7(81.9)
3 21.5(70.7)
4 13.9(57.0)
5 9.6(49.3)
6 5.6(42.1)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Nov
2 20.9(69.6)
3 14.8(58.6)
4 7.7(45.9)
5 4.9(40.8)
6 1.9(35.4)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Dec
2 15.6(60.1)
3 11.2(52.2)
4 4.1(39.4)
5 1.8(35.2)
6 −0.9(30.4)
Climate data for Berlin (Brandenburg), 1991–2020, extremes 1957–2024
1 Year
2 38.4(101.1)
3 34.8(94.6)
4 14.2(57.5)
5 9.9(49.8)
6 5.3(41.6)</code></pre>
<p><strong>Exercise 2.3</strong>: <strong>Data Cleaning</strong> Clean
this data so that mathematical operations can be applied on it. Find the
monthly temperature range.</p>
</div>
<div id="hints-" class="section level3">
<h3>Hints:-</h3>
<ul>
<li><strong>Convert the data into a matrix</strong> 🧮: This allows for
easy vectorization of the cells.</li>
<li><strong>Convert characters to ASCII equivalents</strong> 🔤: Ensure
uniformity in data representation.</li>
<li><strong>Set the row names</strong> 🔍: Use the values in the first
column as row names and the values in the first row as column
names.</li>
<li><strong>Remove commas</strong> 💬: Eliminate commas used as thousand
separators to facilitate numeric conversion.</li>
<li><strong>Extract numeric values</strong> 🔢: Remove any content in
parentheses, including “(°F)” and “(inches)”.</li>
<li><strong>Clean dimension names</strong> 📝: If present, remove “(°F)”
and “(inches)” from the dimension names.</li>
<li><strong>Convert cleaned strings to numeric</strong> ✅: Finally,
ensure that the cleaned strings are of numeric type for analysis.</li>
</ul>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">as.matrix</span>(x) </span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a><span class="co"># converts (“−”) to ASCII equivalents using transliterator. </span></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>x[, ] <span class="ot"><-</span> <span class="fu">stri_trans_general</span>(x, <span class="st">"Publishing-Any; Any-ASCII"</span>) </span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a><span class="co"># Sets the row names to the values in the first column and the column names to the values in the first row. </span></span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a><span class="fu">dimnames</span>(x) <span class="ot"><-</span> <span class="fu">list</span>(x[, <span class="dv">1</span>], x[<span class="dv">1</span>, ]) </span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a><span class="co"># skip 1st/last row and 1st column </span></span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> x[<span class="dv">2</span><span class="sc">:</span>(<span class="fu">nrow</span>(x) <span class="sc">-</span> <span class="dv">1</span>), <span class="dv">2</span><span class="sc">:</span><span class="fu">ncol</span>(x)]</span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a><span class="co"># Displays the first and last columns </span></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a>x[, <span class="fu">c</span>(<span class="dv">1</span>, <span class="fu">ncol</span>(x))] </span></code></pre></div>
<pre><code> Jan
Record high °C (°F) "15.1(59.2)"
Mean maximum °C (°F) "10.6(51.1)"
Mean daily maximum °C (°F) "3.2(37.8)"
Daily mean °C (°F) "0.7(33.3)"
Mean daily minimum °C (°F) "-2.2(28.0)"
Mean minimum °C (°F) "-12.0(10.4)"
Record low °C (°F) "-25.3(-13.5)"
Average precipitation mm (inches) "41.5(1.63)"
Average precipitation days (>= 0.1 mm) "15.8"
Average snowy days (>= 1.0 cm) "8.4"
Average relative humidity (%) "85.9"
Mean monthly sunshine hours "52.6"
Source 1: Data derived from Deutscher Wetterdienst[93] "Source 1: Data derived from Deutscher Wetterdienst[93]"
Year
Record high °C (°F) "38.4(101.1)"
Mean maximum °C (°F) "34.8(94.6)"
Mean daily maximum °C (°F) "14.2(57.5)"
Daily mean °C (°F) "9.9(49.8)"
Mean daily minimum °C (°F) "5.3(41.6)"
Mean minimum °C (°F) "-14.2(6.4)"
Record low °C (°F) "-25.3(-13.5)"
Average precipitation mm (inches) "532.3(20.96)"
Average precipitation days (>= 0.1 mm) "162"
Average snowy days (>= 1.0 cm) "24.3"
Average relative humidity (%) "76.1"
Mean monthly sunshine hours "1,758.6"
Source 1: Data derived from Deutscher Wetterdienst[93] "Source 1: Data derived from Deutscher Wetterdienst[93]"</code></pre>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="co"># remove the last row </span></span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> x[<span class="sc">-</span><span class="fu">nrow</span>(x), ] </span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove commas used as thousand separators</span></span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a>x[,] <span class="ot"><-</span> <span class="fu">stri_replace_all_regex</span>(x, <span class="st">","</span>, <span class="st">""</span>)</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a><span class="co"># Extract numeric values, removing anything in parentheses including "(°F)" and "(inches)" </span></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a>x[,] <span class="ot"><-</span> <span class="fu">stri_replace_all_regex</span>(x, <span class="st">"</span><span class="sc">\\</span><span class="st">s*</span><span class="sc">\\</span><span class="st">(.*?</span><span class="sc">\\</span><span class="st">)|</span><span class="sc">\\</span><span class="st">s*°F|</span><span class="sc">\\</span><span class="st">s*inches"</span>, <span class="st">""</span>)</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove "(°F)" and "(inches)" from the dimension names if they exist</span></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a><span class="fu">dimnames</span>(x)[[<span class="dv">1</span>]] <span class="ot"><-</span> <span class="fu">stri_replace_all_regex</span>(<span class="fu">dimnames</span>(x)[[<span class="dv">1</span>]], <span class="st">"</span><span class="sc">\\</span><span class="st">s*</span><span class="sc">\\</span><span class="st">(°F</span><span class="sc">\\</span><span class="st">)|</span><span class="sc">\\</span><span class="st">s*</span><span class="sc">\\</span><span class="st">(inches</span><span class="sc">\\</span><span class="st">)"</span>, <span class="st">""</span>)</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a><span class="co"># Convert the cleaned strings to numeric</span></span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">apply</span>(x, <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>), <span class="cf">function</span>(val) <span class="fu">as.numeric</span>(val))</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a><span class="co"># Remove the last row</span></span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> x[<span class="sc">-</span><span class="fu">nrow</span>(x), ] </span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(x)</span></code></pre></div>
<pre><code> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov
Record high °C 15.1 19.2 25.8 30.8 32.7 38.4 38.3 38.0 34.1 27.7 20.9
Mean maximum °C 10.6 12.4 17.9 24.0 28.4 31.5 32.7 32.7 26.9 21.5 14.8
Mean daily maximum °C 3.2 4.9 9.0 15.1 19.6 22.9 25.0 24.8 19.8 13.9 7.7
Daily mean °C 0.7 1.6 4.6 9.7 14.2 17.6 19.6 19.2 14.7 9.6 4.9
Mean daily minimum °C -2.2 -1.8 0.4 4.0 8.2 11.7 14.0 13.5 9.8 5.6 1.9
Mean minimum °C -12.0 -9.5 -5.8 -2.6 1.7 6.3 8.9 8.1 3.9 -1.3 -5.0
Dec Year
Record high °C 15.6 38.4
Mean maximum °C 11.2 34.8
Mean daily maximum °C 4.1 14.2
Daily mean °C 1.8 9.9
Mean daily minimum °C -0.9 5.3
Mean minimum °C -8.9 -14.2</code></pre>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Now we can start computation, for example calculation of monthly temperature ranges </span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a>x[<span class="st">"Record high °C"</span>, <span class="sc">-</span><span class="fu">ncol</span>(x)] <span class="sc">-</span> x[<span class="st">"Record low °C"</span>, <span class="sc">-</span><span class="fu">ncol</span>(x)] </span></code></pre></div>
<pre><code> Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
40.4 41.2 44.9 38.2 35.5 37.1 33.4 33.4 35.0 35.4 38.7 39.6 </code></pre>
</div>
</div>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// temporarily add toc-ignore selector to headers for the consistency with Pandoc
$('.unlisted.unnumbered').addClass('toc-ignore')
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>