-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chapter_4.html
1364 lines (1334 loc) · 116 KB
/
Chapter_4.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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Chapter 4: Subsetting</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
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;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="Chapter_4_files/libs/clipboard/clipboard.min.js"></script>
<script src="Chapter_4_files/libs/quarto-html/quarto.js"></script>
<script src="Chapter_4_files/libs/quarto-html/popper.min.js"></script>
<script src="Chapter_4_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="Chapter_4_files/libs/quarto-html/anchor.min.js"></script>
<link href="Chapter_4_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="Chapter_4_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="Chapter_4_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="Chapter_4_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="Chapter_4_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="Chapter_4_files/libs/htmlwidgets-1.6.4/htmlwidgets.js"></script>
<script src="Chapter_4_files/libs/jquery-3.5.1/jquery.min.js"></script>
<script src="Chapter_4_files/libs/proj4js-2.3.15/proj4.js"></script>
<link href="Chapter_4_files/libs/highcharts-9.3.1/css/motion.css" rel="stylesheet">
<script src="Chapter_4_files/libs/highcharts-9.3.1/highcharts.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/highcharts-3d.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/highcharts-more.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/stock.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/map.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/data.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/exporting.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/offline-exporting.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/drilldown.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/item-series.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/overlapping-datalabels.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/annotations.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/export-data.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/funnel.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/heatmap.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/treemap.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/sankey.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/dependency-wheel.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/organization.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/solid-gauge.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/streamgraph.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/sunburst.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/vector.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/wordcloud.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/xrange.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/tilemap.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/venn.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/gantt.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/timeline.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/parallel-coordinates.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/bullet.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/coloraxis.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/dumbbell.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/lollipop.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/series-label.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/plugins/motion.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/custom/reset.js"></script>
<script src="Chapter_4_files/libs/highcharts-9.3.1/modules/boost.js"></script>
<script src="Chapter_4_files/libs/highchart-binding-0.9.4/highchart.js"></script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#subsetting-in-r" id="toc-subsetting-in-r" class="nav-link active" data-scroll-target="#subsetting-in-r"><span class="header-section-number">1</span> Subsetting in R</a>
<ul class="collapse">
<li><a href="#preliminary-concepts" id="toc-preliminary-concepts" class="nav-link" data-scroll-target="#preliminary-concepts"><span class="header-section-number">1.1</span> Preliminary concepts</a></li>
<li><a href="#selecting-multiple-elements" id="toc-selecting-multiple-elements" class="nav-link" data-scroll-target="#selecting-multiple-elements"><span class="header-section-number">1.2</span> Selecting multiple elements</a></li>
<li><a href="#selecting-single-elements" id="toc-selecting-single-elements" class="nav-link" data-scroll-target="#selecting-single-elements"><span class="header-section-number">1.3</span> Selecting single elements</a></li>
<li><a href="#subsetting-and-assignment--subassignment" id="toc-subsetting-and-assignment--subassignment" class="nav-link" data-scroll-target="#subsetting-and-assignment--subassignment"><span class="header-section-number">1.4</span> Subsetting and assignment -Subassignment</a></li>
<li><a href="#comparing-speed-and-performance" id="toc-comparing-speed-and-performance" class="nav-link" data-scroll-target="#comparing-speed-and-performance"><span class="header-section-number">1.5</span> Comparing speed and performance</a></li>
<li><a href="#subsetting---some-applications" id="toc-subsetting---some-applications" class="nav-link" data-scroll-target="#subsetting---some-applications"><span class="header-section-number">1.6</span> Subsetting - Some applications</a></li>
</ul></li>
</ul>
</nav>
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Chapter 4: Subsetting</h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<section id="subsetting-in-r" class="level2" data-number="1">
<h2 data-number="1" class="anchored" data-anchor-id="subsetting-in-r"><span class="header-section-number">1</span> Subsetting in R</h2>
<section id="preliminary-concepts" class="level3" data-number="1.1">
<h3 data-number="1.1" class="anchored" data-anchor-id="preliminary-concepts"><span class="header-section-number">1.1</span> Preliminary concepts</h3>
<p>Subsetting allows you to pull out the pieces of an object you are interested in.</p>
<p><strong>6 ways</strong> of subsetting atomic vectors</p>
<ul>
<li>Positive integers*</li>
<li>Negative integers</li>
<li>Logical vectors</li>
<li>Nothing</li>
<li>Zero</li>
<li>Character vectors</li>
</ul>
<p>(*Factors are <em>not</em> treated specially -subsetting uses the underlying integer vector)</p>
<p><strong>3 operators:</strong></p>
<ol type="1">
<li>[</li>
<li>[[</li>
<li>$</li>
</ol>
<p><strong>Subsetting</strong> can be combined <strong>with</strong> <strong>assignment</strong></p>
<p>⏩ OVERVIEW:</p>
<ul>
<li><p>Subsetting <strong>multiple elements</strong></p></li>
<li><p>Subsetting <strong>single elements</strong></p></li>
<li><p>Subsetting <strong>with assignment</strong></p></li>
<li><p>Further <strong>applications</strong></p></li>
</ul>
</section>
<section id="selecting-multiple-elements" class="level3" data-number="1.2">
<h3 data-number="1.2" class="anchored" data-anchor-id="selecting-multiple-elements"><span class="header-section-number">1.2</span> Selecting multiple elements</h3>
<section id="atomic-vectors" class="level4" data-number="1.2.1">
<h4 data-number="1.2.1" class="anchored" data-anchor-id="atomic-vectors"><span class="header-section-number">1.2.1</span> Atomic Vectors</h4>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="fl">2.1</span>, <span class="fl">4.2</span>, <span class="fl">3.3</span>, <span class="fl">5.4</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<ol type="1">
<li>With <strong>positive integers</strong></li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Return element at specified position</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">1</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3.3 2.1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Duplicate indices will duplicate values</span></span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.1 2.1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Real numbers are silently truncated to integers</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="fl">2.1</span>, <span class="fl">2.9</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4.2 4.2</code></pre>
</div>
</div>
<ol start="2" type="1">
<li>With <strong>negative integers</strong></li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb8"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" aria-hidden="true" tabindex="-1"></a><span class="co"># To exclude elements at specified positions </span></span>
<span id="cb8-2"><a href="#cb8-2" aria-hidden="true" tabindex="-1"></a>x[<span class="sc">-</span><span class="fu">c</span>(<span class="dv">3</span>, <span class="dv">1</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 4.2 5.4</code></pre>
</div>
</div>
<p>Note that they cannot be mixed with positive integers</p>
<ol start="3" type="1">
<li>With <strong>logical vectors</strong></li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Return element where the corresponding logical value is TRUE</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">TRUE</span>, <span class="cn">FALSE</span>, <span class="cn">FALSE</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.1 4.2</code></pre>
</div>
</div>
<p>When the logical vector contains a missing value (NA):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb12"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Missing value in the index returns a missing value also in the output </span></span>
<span id="cb12-2"><a href="#cb12-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">TRUE</span>, <span class="cn">NA</span>, <span class="cn">FALSE</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.1 4.2 NA</code></pre>
</div>
</div>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Recycling rules</strong>: when you subset a vector using a vector of a different length, R recycles the shorter of the two to the length of the longer</p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb14"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.1 3.3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb16"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Equivalent to </span></span>
<span id="cb16-2"><a href="#cb16-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>, <span class="cn">TRUE</span>, <span class="cn">FALSE</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.1 3.3</code></pre>
</div>
</div>
<ol start="4" type="1">
<li>With <strong>nothing</strong></li>
</ol>
<p>More useful for 2D objects (e.g., data frames) than for 1D vectors</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Return the original vector </span></span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a>x[]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2.1 4.2 3.3 5.4</code></pre>
</div>
</div>
<ol start="5" type="1">
<li>With <strong>character vectors</strong></li>
</ol>
<p>To be used with named vectors</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Return element with matching names </span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a>(y <span class="ot"><-</span> <span class="fu">setNames</span>(x, letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">4</span>]))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> a b c d
2.1 4.2 3.3 5.4 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a>y[<span class="fu">c</span>(<span class="st">"a"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> a
2.1 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Like integer indices, you can repeat indices</span></span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a>y[<span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"a"</span>, <span class="st">"a"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> a a a
2.1 2.1 2.1 </code></pre>
</div>
</div>
<p>When subsetting with [, names are always matched exactly..if no match, return NA</p>
<ol start="6" type="1">
<li>With <strong>zero</strong></li>
</ol>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Returns a zero-length vector</span></span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a>x[<span class="dv">0</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>numeric(0)</code></pre>
</div>
</div>
</section>
<section id="lists" class="level4" data-number="1.2.2">
<h4 data-number="1.2.2" class="anchored" data-anchor-id="lists"><span class="header-section-number">1.2.2</span> Lists</h4>
<p>same way as subsetting an atomic vector.</p>
<ul>
<li>[ always returns a list</li>
<li>[[ and $ pull out elements of a list</li>
</ul>
</section>
<section id="higher-dimensional-objects" class="level4" data-number="1.2.3">
<h4 data-number="1.2.3" class="anchored" data-anchor-id="higher-dimensional-objects"><span class="header-section-number">1.2.3</span> Higher dimensional objects</h4>
<p><strong>3 ways of subsetting</strong>:</p>
<p>1. With multiple vectors</p>
<p>2. With single vector</p>
<p>3. With a matrix</p>
<p><strong>Matrices (2D) and arrays (>2D)</strong></p>
<p>Supply an index for each dimension separated by comma</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb28"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb28-1"><a href="#cb28-1" aria-hidden="true" tabindex="-1"></a>a <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">9</span>, <span class="at">nrow =</span> <span class="dv">3</span>) </span>
<span id="cb28-2"><a href="#cb28-2" aria-hidden="true" tabindex="-1"></a><span class="fu">colnames</span>(a) <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"C"</span>)</span>
<span id="cb28-3"><a href="#cb28-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb28-4"><a href="#cb28-4" aria-hidden="true" tabindex="-1"></a>a[<span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A B C
[1,] 1 4 7
[2,] 2 5 8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb30"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb30-1"><a href="#cb30-1" aria-hidden="true" tabindex="-1"></a>a[<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>, <span class="cn">TRUE</span>), <span class="fu">c</span>(<span class="st">"B"</span>, <span class="st">"A"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> B A
[1,] 4 1
[2,] 6 3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb32"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" aria-hidden="true" tabindex="-1"></a>a[<span class="dv">0</span>, <span class="sc">-</span><span class="dv">2</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> A C</code></pre>
</div>
</div>
<p>In R, subsetting a matrix (or array) using a single vector of indices treats the matrix as if it were a one-dimensional vector in column-major order (i.e., column-wise order). This means it traverses the matrix column by column, rather than row by row.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb34"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb34-1"><a href="#cb34-1" aria-hidden="true" tabindex="-1"></a>vals <span class="ot"><-</span> <span class="fu">outer</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>, <span class="at">FUN =</span> <span class="st">"paste"</span>, <span class="at">sep =</span> <span class="st">","</span>) </span>
<span id="cb34-2"><a href="#cb34-2" aria-hidden="true" tabindex="-1"></a>vals</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [,1] [,2] [,3] [,4] [,5]
[1,] "1,1" "1,2" "1,3" "1,4" "1,5"
[2,] "2,1" "2,2" "2,3" "2,4" "2,5"
[3,] "3,1" "3,2" "3,3" "3,4" "3,5"
[4,] "4,1" "4,2" "4,3" "4,4" "4,5"
[5,] "5,1" "5,2" "5,3" "5,4" "5,5"</code></pre>
</div>
<div class="sourceCode cell-code" id="cb36"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb36-1"><a href="#cb36-1" aria-hidden="true" tabindex="-1"></a>vals[<span class="fu">c</span>(<span class="dv">4</span>, <span class="dv">15</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "4,1" "5,3"</code></pre>
</div>
</div>
<p>You can also subset higher-dimensional data structures with an integer matrix</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb38"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb38-1"><a href="#cb38-1" aria-hidden="true" tabindex="-1"></a>select <span class="ot"><-</span> <span class="fu">matrix</span>(<span class="at">ncol =</span> <span class="dv">2</span>, <span class="at">byrow =</span> <span class="cn">TRUE</span>, <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">1</span>, <span class="dv">3</span>, <span class="dv">1</span>,<span class="dv">2</span>, <span class="dv">4</span> ))</span>
<span id="cb38-2"><a href="#cb38-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb38-3"><a href="#cb38-3" aria-hidden="true" tabindex="-1"></a>vals[select]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "1,1" "3,1" "2,4"</code></pre>
</div>
</div>
</section>
<section id="data-frames-and-tibbles" class="level4" data-number="1.2.4">
<h4 data-number="1.2.4" class="anchored" data-anchor-id="data-frames-and-tibbles"><span class="header-section-number">1.2.4</span> <strong>Data frames and tibbles</strong></h4>
<ul>
<li>When subsetting with a single index, they behave like lists and index the columns</li>
<li>When subsetting with two indices, they behave like matrices</li>
</ul>
<div class="cell">
<div class="sourceCode cell-code" id="cb40"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb40-1"><a href="#cb40-1" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">y =</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">z =</span> letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>]) </span>
<span id="cb40-2"><a href="#cb40-2" aria-hidden="true" tabindex="-1"></a>df[df<span class="sc">$</span>x <span class="sc">==</span> <span class="dv">2</span>, ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> x y z
2 2 2 b</code></pre>
</div>
<div class="sourceCode cell-code" id="cb42"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb42-1"><a href="#cb42-1" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">3</span>), ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> x y z
1 1 3 a
3 3 1 c</code></pre>
</div>
<div class="sourceCode cell-code" id="cb44"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb44-1"><a href="#cb44-1" aria-hidden="true" tabindex="-1"></a><span class="co"># There are two ways to select columns from a data frame # Like a list</span></span>
<span id="cb44-2"><a href="#cb44-2" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">c</span>(<span class="st">"x"</span>, <span class="st">"z"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> x z
1 1 a
2 2 b
3 3 c</code></pre>
</div>
<div class="sourceCode cell-code" id="cb46"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb46-1"><a href="#cb46-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Like a matrix df[, c("x", "z")]</span></span>
<span id="cb46-2"><a href="#cb46-2" aria-hidden="true" tabindex="-1"></a>df[, <span class="fu">c</span>(<span class="st">"x"</span>, <span class="st">"z"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> x z
1 1 a
2 2 b
3 3 c</code></pre>
</div>
<div class="sourceCode cell-code" id="cb48"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb48-1"><a href="#cb48-1" aria-hidden="true" tabindex="-1"></a><span class="co"># There's an important difference if you select a single</span></span>
<span id="cb48-2"><a href="#cb48-2" aria-hidden="true" tabindex="-1"></a><span class="co"># column: matrix subsetting simplifies by default, list</span></span>
<span id="cb48-3"><a href="#cb48-3" aria-hidden="true" tabindex="-1"></a><span class="co"># subsetting does not.</span></span>
<span id="cb48-4"><a href="#cb48-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb48-5"><a href="#cb48-5" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(df[<span class="st">"x"</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame': 3 obs. of 1 variable:
$ x: int 1 2 3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb50"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb50-1"><a href="#cb50-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(df[, <span class="st">"x"</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> int [1:3] 1 2 3</code></pre>
</div>
</div>
</section>
<section id="dimensionality" class="level4" data-number="1.2.5">
<h4 data-number="1.2.5" class="anchored" data-anchor-id="dimensionality"><span class="header-section-number">1.2.5</span> <strong>Dimensionality</strong></h4>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p><strong>Dimensionality reduction</strong>: when you subset with [, [ simplifies the results to the lowest possible dimensionality.</p>
</div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb52"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb52-1"><a href="#cb52-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Both return a 1D vector</span></span>
<span id="cb52-2"><a href="#cb52-2" aria-hidden="true" tabindex="-1"></a>a[<span class="dv">1</span>, ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>A B C
1 4 7 </code></pre>
</div>
<div class="sourceCode cell-code" id="cb54"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb54-1"><a href="#cb54-1" aria-hidden="true" tabindex="-1"></a>a[<span class="dv">1</span>, <span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>A
1 </code></pre>
</div>
</div>
<p>To preserve the original dimensionality, you must use drop = FALSE</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb56"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb56-1"><a href="#cb56-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Example with data frames</span></span>
<span id="cb56-2"><a href="#cb56-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">a =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>, <span class="at">b =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">2</span>)</span>
<span id="cb56-3"><a href="#cb56-3" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(df[, <span class="st">"a"</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> int [1:2] 1 2</code></pre>
</div>
<div class="sourceCode cell-code" id="cb58"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb58-1"><a href="#cb58-1" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(df[, <span class="st">"a"</span>, <span class="at">drop =</span> <span class="cn">FALSE</span>])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>'data.frame': 2 obs. of 1 variable:
$ a: int 1 2</code></pre>
</div>
</div>
<p>✳️ Why can ‘drop = TRUE’ by default be a problem?</p>
<ul>
<li>When you subset a 2D matrix or dataframe and the result has only one row or column, R simplifies the result to the lowest possible dimensionality --> from 2D df to 1D integer vector. This is a problem when e.g., you write a function that works on df (of 2D), but after subsetting it doesn’t work anymore -because the df became a 1D vector.</li>
</ul>
<p>Factor subsetting also has a drop argument, but it controls whether or not levels are preserved, default is drop = FALSE</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb60"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb60-1"><a href="#cb60-1" aria-hidden="true" tabindex="-1"></a>z <span class="ot"><-</span> <span class="fu">factor</span>(<span class="fu">c</span>(<span class="st">"a"</span>, <span class="st">"b"</span>))</span>
<span id="cb60-2"><a href="#cb60-2" aria-hidden="true" tabindex="-1"></a>z[<span class="dv">1</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] a
Levels: a b</code></pre>
</div>
<div class="sourceCode cell-code" id="cb62"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb62-1"><a href="#cb62-1" aria-hidden="true" tabindex="-1"></a>z[<span class="dv">1</span>, drop <span class="ot">=</span> <span class="cn">TRUE</span>]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] a
Levels: a</code></pre>
</div>
</div>
</section>
</section>
<section id="selecting-single-elements" class="level3" data-number="1.3">
<h3 data-number="1.3" class="anchored" data-anchor-id="selecting-single-elements"><span class="header-section-number">1.3</span> Selecting single elements</h3>
<section id="operators" class="level4" data-number="1.3.1">
<h4 data-number="1.3.1" class="anchored" data-anchor-id="operators"><span class="header-section-number">1.3.1</span> <strong>2 operators:</strong></h4>
<ul>
<li><p>[[ extracts a single item</p></li>
<li><p>$ shortcut for x[[“y”]], commonly used to access var in df</p></li>
</ul>
<p><strong>[[</strong> important when working with lists. As opposed to [ -which always return a <em>smaller</em> list- it extract the element itself.</p>
<p>Recommended for atomic vectors too when getting single value</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb64"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb64-1"><a href="#cb64-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">list</span>(<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="st">"a"</span>, <span class="dv">4</span><span class="sc">:</span><span class="dv">6</span>)</span>
<span id="cb64-2"><a href="#cb64-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb64-3"><a href="#cb64-3" aria-hidden="true" tabindex="-1"></a><span class="co"># How to subset a single element? </span></span>
<span id="cb64-4"><a href="#cb64-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Etiher a list OR the content - this is the difference between [ and [[</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p><strong>NOTE:</strong></p>
<p>Because [[ can return only a single item, you must use it with either a single positive integer or a single string. If you use a vector with [[ , it will <em>subset recursively,</em> i.e. x[[c(1, 2)]] is equivalent to x[[1]][[2]] .</p>
</section>
<section id="useful-comparisons-between-and" class="level4" data-number="1.3.2">
<h4 data-number="1.3.2" class="anchored" data-anchor-id="useful-comparisons-between-and"><span class="header-section-number">1.3.2</span> <strong>Useful comparisons between $ and [[:</strong></h4>
<p>❗ It is a mistake to use it when you have the col name stored in a variable</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb65"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb65-1"><a href="#cb65-1" aria-hidden="true" tabindex="-1"></a>var <span class="ot"><-</span> <span class="st">"cyl"</span> <span class="co"># Doesn't work - mtcars$var translated to mtcars[["var"]]</span></span>
<span id="cb65-2"><a href="#cb65-2" aria-hidden="true" tabindex="-1"></a>mtcars<span class="sc">$</span>var</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
<div class="sourceCode cell-code" id="cb67"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb67-1"><a href="#cb67-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Instead use [[</span></span>
<span id="cb67-2"><a href="#cb67-2" aria-hidden="true" tabindex="-1"></a>mtcars[[var]]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4</code></pre>
</div>
</div>
<p>❗ *</p>
<p>This is about how $ and [[ handle element names; e.g., useful whit named lists. The <strong>$</strong> operator allows for <strong>partial matching</strong> of element names within a list or data frame. This means that if you provide an incomplete name that matches the beginning of a longer name, $ will still return a result. The <strong>[[</strong> operator requires an <strong>exact match</strong> of the element name</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb69"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb69-1"><a href="#cb69-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">abc =</span> <span class="dv">1</span>) </span>
<span id="cb69-2"><a href="#cb69-2" aria-hidden="true" tabindex="-1"></a>x<span class="sc">$</span>a</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb71"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb71-1"><a href="#cb71-1" aria-hidden="true" tabindex="-1"></a>x[[<span class="st">"a"</span>]]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>NULL</code></pre>
</div>
<div class="sourceCode cell-code" id="cb73"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb73-1"><a href="#cb73-1" aria-hidden="true" tabindex="-1"></a><span class="co"># To avoid this behaviour set the global option warnPartialMatchDollar to TRUE </span></span>
<span id="cb73-2"><a href="#cb73-2" aria-hidden="true" tabindex="-1"></a><span class="co"># Also, tibbles never do partial matching as opposed to df </span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="missing-and-out-of-bound-indices" class="level4" data-number="1.3.3">
<h4 data-number="1.3.3" class="anchored" data-anchor-id="missing-and-out-of-bound-indices"><span class="header-section-number">1.3.3</span> Missing and out-of-bound indices</h4>
<p>What happens with [[ when you use an “invalid” index?</p>
<p>You can get inconsistent results -it depends on the object you are subsetting and the index used</p>
<p><img src="oob_recap.png" class="img-fluid"></p>
<div class="cell">
<div class="sourceCode cell-code" id="cb74"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb74-1"><a href="#cb74-1" aria-hidden="true" tabindex="-1"></a><span class="co"># When the element is missing an alternative is to rely on purrr</span></span>
<span id="cb74-2"><a href="#cb74-2" aria-hidden="true" tabindex="-1"></a><span class="co"># purrr::pluck() -always returns NULL</span></span>
<span id="cb74-3"><a href="#cb74-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb74-4"><a href="#cb74-4" aria-hidden="true" tabindex="-1"></a><span class="co"># purrr::chuck() -always throw error</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Examples:</p>
<ul>
<li>Zero-length object: This could be NULL or logical()</li>
<li>Out-of-bounds values (OOB): Indices that exceed the length of the vector or list</li>
<li>Missing values: Such as NA_integer_</li>
</ul>
</section>
<section id="and-slot" class="level4" data-number="1.3.4">
<h4 data-number="1.3.4" class="anchored" data-anchor-id="and-slot"><span class="header-section-number">1.3.4</span> @ and slot()</h4>
<p>@: equivalent to $ for S4 objects</p>
<p>slot(): equivalent to [[ for S4 objects</p>
</section>
</section>
<section id="subsetting-and-assignment--subassignment" class="level3" data-number="1.4">
<h3 data-number="1.4" class="anchored" data-anchor-id="subsetting-and-assignment--subassignment"><span class="header-section-number">1.4</span> Subsetting and assignment -Subassignment</h3>
<p>Example of modifying elements of a vector</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb75"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb75-1"><a href="#cb75-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">5</span></span>
<span id="cb75-2"><a href="#cb75-2" aria-hidden="true" tabindex="-1"></a>x[<span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>)] <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">101</span>, <span class="dv">102</span>)</span>
<span id="cb75-3"><a href="#cb75-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb75-4"><a href="#cb75-4" aria-hidden="true" tabindex="-1"></a><span class="co"># recommend that you make sure that length(value) is the same as length(x[i]) , and that i is unique (R will recycle if needed and the rules can be complex)</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>Example of removing element from a list</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb76"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb76-1"><a href="#cb76-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">a =</span> <span class="dv">1</span>, <span class="at">b =</span> <span class="dv">2</span>)</span>
<span id="cb76-2"><a href="#cb76-2" aria-hidden="true" tabindex="-1"></a>x[[<span class="st">"b"</span>]] <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb76-3"><a href="#cb76-3" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(x)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>List of 1
$ a: num 1</code></pre>
</div>
<div class="sourceCode cell-code" id="cb78"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb78-1"><a href="#cb78-1" aria-hidden="true" tabindex="-1"></a><span class="co"># To add a literal NULL</span></span>
<span id="cb78-2"><a href="#cb78-2" aria-hidden="true" tabindex="-1"></a>y <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">a =</span> <span class="dv">1</span>, <span class="at">b =</span> <span class="dv">2</span>)</span>
<span id="cb78-3"><a href="#cb78-3" aria-hidden="true" tabindex="-1"></a>y[<span class="st">"b"</span>] <span class="ot"><-</span> <span class="fu">list</span>(<span class="cn">NULL</span>)</span>
<span id="cb78-4"><a href="#cb78-4" aria-hidden="true" tabindex="-1"></a><span class="fu">str</span>(y)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>List of 2
$ a: num 1
$ b: NULL</code></pre>
</div>
</div>
<p>Subsetting with nothing can be useful with assignment because it preserves the structure of the original object.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb80"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb80-1"><a href="#cb80-1" aria-hidden="true" tabindex="-1"></a><span class="co"># here it's changing the content of mtcars</span></span>
<span id="cb80-2"><a href="#cb80-2" aria-hidden="true" tabindex="-1"></a>mtcars[] <span class="ot"><-</span> <span class="fu">lapply</span>(mtcars, as.integer) </span>
<span id="cb80-3"><a href="#cb80-3" aria-hidden="true" tabindex="-1"></a><span class="fu">is.data.frame</span>(mtcars)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb82"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb82-1"><a href="#cb82-1" aria-hidden="true" tabindex="-1"></a><span class="co"># here it's changing the object mtcars is bound to</span></span>
<span id="cb82-2"><a href="#cb82-2" aria-hidden="true" tabindex="-1"></a>mtcars <span class="ot"><-</span> <span class="fu">lapply</span>(mtcars, as.integer)</span>
<span id="cb82-3"><a href="#cb82-3" aria-hidden="true" tabindex="-1"></a><span class="fu">is.data.frame</span>(mtcars)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] FALSE</code></pre>
</div>
</div>
</section>
<section id="comparing-speed-and-performance" class="level3" data-number="1.5">
<h3 data-number="1.5" class="anchored" data-anchor-id="comparing-speed-and-performance"><span class="header-section-number">1.5</span> Comparing speed and performance</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb84"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb84-1"><a href="#cb84-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(data.table)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'data.table' was built under R version 4.3.3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb86"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb86-1"><a href="#cb86-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tibble)</span>
<span id="cb86-2"><a href="#cb86-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(collapse)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>collapse 2.0.15, see ?`collapse-package` or ?`collapse-documentation`</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>
Attaching package: 'collapse'</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following object is masked from 'package:data.table':
fdroplevels</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>The following object is masked from 'package:stats':
D</code></pre>
</div>
<div class="sourceCode cell-code" id="cb91"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb91-1"><a href="#cb91-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(microbenchmark)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'microbenchmark' was built under R version 4.3.3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb93"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb93-1"><a href="#cb93-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(highcharter)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: package 'highcharter' was built under R version 4.3.3</code></pre>
</div>
<div class="cell-output cell-output-stderr">
<pre><code>Registered S3 method overwritten by 'quantmod':
method from
as.zoo.data.frame zoo </code></pre>
</div>
<div class="sourceCode cell-code" id="cb96"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb96-1"><a href="#cb96-1" aria-hidden="true" tabindex="-1"></a>size <span class="ot"><-</span> <span class="fl">1e5</span></span>
<span id="cb96-2"><a href="#cb96-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb96-3"><a href="#cb96-3" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(</span>
<span id="cb96-4"><a href="#cb96-4" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="fu">runif</span>(size),</span>
<span id="cb96-5"><a href="#cb96-5" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">runif</span>(size),</span>
<span id="cb96-6"><a href="#cb96-6" aria-hidden="true" tabindex="-1"></a> <span class="at">id =</span> <span class="fu">sample</span>(letters, size, <span class="at">replace =</span> <span class="cn">TRUE</span>)</span>
<span id="cb96-7"><a href="#cb96-7" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb96-8"><a href="#cb96-8" aria-hidden="true" tabindex="-1"></a>dt <span class="ot"><-</span> <span class="fu">as.data.table</span>(df)</span>
<span id="cb96-9"><a href="#cb96-9" aria-hidden="true" tabindex="-1"></a>tb <span class="ot"><-</span> <span class="fu">as_tibble</span>(df)</span>
<span id="cb96-10"><a href="#cb96-10" aria-hidden="true" tabindex="-1"></a>lst <span class="ot"><-</span> <span class="fu">list</span>(<span class="at">x =</span> <span class="fu">runif</span>(size), </span>
<span id="cb96-11"><a href="#cb96-11" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">runif</span>(size), </span>
<span id="cb96-12"><a href="#cb96-12" aria-hidden="true" tabindex="-1"></a> <span class="at">id =</span> <span class="fu">sample</span>(letters, size, <span class="at">replace =</span> <span class="cn">TRUE</span>))</span>
<span id="cb96-13"><a href="#cb96-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb96-14"><a href="#cb96-14" aria-hidden="true" tabindex="-1"></a><span class="co"># Benchmarking various subsetting operations</span></span>
<span id="cb96-15"><a href="#cb96-15" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb96-16"><a href="#cb96-16" aria-hidden="true" tabindex="-1"></a>bench <span class="ot"><-</span> microbenchmark<span class="sc">::</span><span class="fu">microbenchmark</span>(</span>
<span id="cb96-17"><a href="#cb96-17" aria-hidden="true" tabindex="-1"></a> <span class="at">times =</span> <span class="dv">100</span>,</span>
<span id="cb96-18"><a href="#cb96-18" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-19"><a href="#cb96-19" aria-hidden="true" tabindex="-1"></a> <span class="co"># Base R subsetting for data.frame</span></span>
<span id="cb96-20"><a href="#cb96-20" aria-hidden="true" tabindex="-1"></a> <span class="at">df_base =</span> {</span>
<span id="cb96-21"><a href="#cb96-21" aria-hidden="true" tabindex="-1"></a> df[df<span class="sc">$</span>x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> df<span class="sc">$</span>y <span class="sc"><</span> <span class="fl">0.5</span>, ]</span>
<span id="cb96-22"><a href="#cb96-22" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb96-23"><a href="#cb96-23" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-24"><a href="#cb96-24" aria-hidden="true" tabindex="-1"></a> <span class="co"># Base R subsetting for data.table</span></span>
<span id="cb96-25"><a href="#cb96-25" aria-hidden="true" tabindex="-1"></a> <span class="at">dt_base =</span> {</span>
<span id="cb96-26"><a href="#cb96-26" aria-hidden="true" tabindex="-1"></a> dt[dt<span class="sc">$</span>x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> dt<span class="sc">$</span>y <span class="sc"><</span> <span class="fl">0.5</span>]</span>
<span id="cb96-27"><a href="#cb96-27" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb96-28"><a href="#cb96-28" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-29"><a href="#cb96-29" aria-hidden="true" tabindex="-1"></a> <span class="co"># Base R subsetting for tibble</span></span>
<span id="cb96-30"><a href="#cb96-30" aria-hidden="true" tabindex="-1"></a> <span class="at">tb_base =</span> {</span>
<span id="cb96-31"><a href="#cb96-31" aria-hidden="true" tabindex="-1"></a> tb[tb<span class="sc">$</span>x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> tb<span class="sc">$</span>y <span class="sc"><</span> <span class="fl">0.5</span>, ]</span>
<span id="cb96-32"><a href="#cb96-32" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb96-33"><a href="#cb96-33" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-34"><a href="#cb96-34" aria-hidden="true" tabindex="-1"></a> <span class="co"># Using collapse package with data.frame</span></span>
<span id="cb96-35"><a href="#cb96-35" aria-hidden="true" tabindex="-1"></a> <span class="at">df_clp =</span> {</span>
<span id="cb96-36"><a href="#cb96-36" aria-hidden="true" tabindex="-1"></a> df <span class="sc">|></span> <span class="fu">fsubset</span>(x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> y <span class="sc"><</span> <span class="fl">0.5</span>)</span>
<span id="cb96-37"><a href="#cb96-37" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb96-38"><a href="#cb96-38" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-39"><a href="#cb96-39" aria-hidden="true" tabindex="-1"></a> <span class="co"># Using collapse package with data.table</span></span>
<span id="cb96-40"><a href="#cb96-40" aria-hidden="true" tabindex="-1"></a> <span class="at">dt_clp =</span> {</span>
<span id="cb96-41"><a href="#cb96-41" aria-hidden="true" tabindex="-1"></a> dt <span class="sc">|></span> <span class="fu">fsubset</span>(x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> y <span class="sc"><</span> <span class="fl">0.5</span>)</span>
<span id="cb96-42"><a href="#cb96-42" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb96-43"><a href="#cb96-43" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-44"><a href="#cb96-44" aria-hidden="true" tabindex="-1"></a> <span class="co"># Using collapse package with tibble</span></span>
<span id="cb96-45"><a href="#cb96-45" aria-hidden="true" tabindex="-1"></a> <span class="at">tb_clp =</span> {</span>
<span id="cb96-46"><a href="#cb96-46" aria-hidden="true" tabindex="-1"></a> tb <span class="sc">|></span> <span class="fu">fsubset</span>(x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> y <span class="sc"><</span> <span class="fl">0.5</span>)</span>
<span id="cb96-47"><a href="#cb96-47" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb96-48"><a href="#cb96-48" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-49"><a href="#cb96-49" aria-hidden="true" tabindex="-1"></a> <span class="co"># Using data.table package with data.table</span></span>
<span id="cb96-50"><a href="#cb96-50" aria-hidden="true" tabindex="-1"></a> <span class="at">dt_dt =</span> {</span>
<span id="cb96-51"><a href="#cb96-51" aria-hidden="true" tabindex="-1"></a> dt[x <span class="sc">></span> <span class="fl">0.5</span> <span class="sc">&</span> y <span class="sc"><</span> <span class="fl">0.5</span>]</span>
<span id="cb96-52"><a href="#cb96-52" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb96-53"><a href="#cb96-53" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-54"><a href="#cb96-54" aria-hidden="true" tabindex="-1"></a> <span class="co"># Subsetting lists using base R</span></span>
<span id="cb96-55"><a href="#cb96-55" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb96-56"><a href="#cb96-56" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb96-57"><a href="#cb96-57" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb96-58"><a href="#cb96-58" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">requireNamespace</span>(<span class="st">"highcharter"</span>)) {</span>
<span id="cb96-59"><a href="#cb96-59" aria-hidden="true" tabindex="-1"></a> hc_dt <span class="ot"><-</span> highcharter<span class="sc">::</span><span class="fu">data_to_boxplot</span>(bench,</span>
<span id="cb96-60"><a href="#cb96-60" aria-hidden="true" tabindex="-1"></a> time,</span>
<span id="cb96-61"><a href="#cb96-61" aria-hidden="true" tabindex="-1"></a> expr,</span>
<span id="cb96-62"><a href="#cb96-62" aria-hidden="true" tabindex="-1"></a> <span class="at">add_outliers =</span> <span class="cn">FALSE</span>,</span>
<span id="cb96-63"><a href="#cb96-63" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="st">"Time in milliseconds"</span>)</span>
<span id="cb96-64"><a href="#cb96-64" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb96-65"><a href="#cb96-65" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">highchart</span>() <span class="sc">|></span></span>
<span id="cb96-66"><a href="#cb96-66" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">hc_xAxis</span>(<span class="at">type =</span> <span class="st">"category"</span>) <span class="sc">|></span></span>
<span id="cb96-67"><a href="#cb96-67" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">hc_chart</span>(<span class="at">inverted =</span> <span class="cn">TRUE</span>) <span class="sc">|></span></span>
<span id="cb96-68"><a href="#cb96-68" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">hc_add_series_list</span>(hc_dt)</span>
<span id="cb96-69"><a href="#cb96-69" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb96-70"><a href="#cb96-70" aria-hidden="true" tabindex="-1"></a> <span class="fu">boxplot</span>(bench, <span class="at">outline =</span> <span class="cn">FALSE</span>)</span>
<span id="cb96-71"><a href="#cb96-71" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="highchart html-widget html-fill-item" id="htmlwidget-41cc37ca17953fa7371c" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-41cc37ca17953fa7371c">{"x":{"hc_opts":{"chart":{"reflow":true,"inverted":true},"title":{"text":null},"yAxis":{"title":{"text":null}},"credits":{"enabled":false},"exporting":{"enabled":false},"boost":{"enabled":false},"plotOptions":{"series":{"label":{"enabled":false},"turboThreshold":0},"treemap":{"layoutAlgorithm":"squarified"}},"xAxis":{"type":"category"},"series":[{"name":"Time in milliseconds","data":[{"name":"df_base","low":2742700,"q1":3060900,"median":3623700,"q3":5327750,"high":8477100},{"name":"dt_base","low":1522600,"q1":1872050,"median":2104200,"q3":2516250,"high":3462900},{"name":"tb_base","low":1270000,"q1":1537200,"median":1696250,"q3":2012250,"high":2497100},{"name":"df_clp","low":1103900,"q1":1405050,"median":1582050,"q3":2119750,"high":3165400},{"name":"dt_clp","low":1091400,"q1":1405750,"median":1549150,"q3":1989300,"high":2699500},{"name":"tb_clp","low":1020800,"q1":1388400,"median":1559650,"q3":1918300,"high":2649300},{"name":"dt_dt","low":1608600,"q1":1923800,"median":2105850,"q3":2667550,"high":3711500}],"id":null,"type":"boxplot"}]},"theme":{"chart":{"backgroundColor":"transparent"},"colors":["#7cb5ec","#434348","#90ed7d","#f7a35c","#8085e9","#f15c80","#e4d354","#2b908f","#f45b5b","#91e8e1"]},"conf_opts":{"global":{"Date":null,"VMLRadialGradientURL":"http =//code.highcharts.com/list(version)/gfx/vml-radial-gradient.png","canvasToolsURL":"http =//code.highcharts.com/list(version)/modules/canvas-tools.js","getTimezoneOffset":null,"timezoneOffset":0,"useUTC":true},"lang":{"contextButtonTitle":"Chart context menu","decimalPoint":".","downloadCSV":"Download CSV","downloadJPEG":"Download JPEG image","downloadPDF":"Download PDF document","downloadPNG":"Download PNG image","downloadSVG":"Download SVG vector image","downloadXLS":"Download XLS","drillUpText":"◁ Back to {series.name}","exitFullscreen":"Exit from full screen","exportData":{"annotationHeader":"Annotations","categoryDatetimeHeader":"DateTime","categoryHeader":"Category"},"hideData":"Hide data table","invalidDate":null,"loading":"Loading...","months":["January","February","March","April","May","June","July","August","September","October","November","December"],"noData":"No data to display","numericSymbolMagnitude":1000,"numericSymbols":["k","M","G","T","P","E"],"printChart":"Print chart","resetZoom":"Reset zoom","resetZoomTitle":"Reset zoom level 1:1","shortMonths":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"shortWeekdays":["Sat","Sun","Mon","Tue","Wed","Thu","Fri"],"thousandsSep":" ","viewData":"View data table","viewFullscreen":"View in full screen","weekdays":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]}},"type":"chart","fonts":[],"debug":false},"evals":[],"jsHooks":[]}</script>
</div>
</div>
<section id="boolean-algebra-vs-sets-i.e.-logical-vs-integer-subsetting" class="level4" data-number="1.5.1">
<h4 data-number="1.5.1" class="anchored" data-anchor-id="boolean-algebra-vs-sets-i.e.-logical-vs-integer-subsetting"><span class="header-section-number">1.5.1</span> Boolean algebra vs sets (i.e., logical vs integer subsetting)</h4>
<div class="callout callout-style-default callout-note callout-titled">
<div class="callout-header d-flex align-content-center">
<div class="callout-icon-container">
<i class="callout-icon"></i>
</div>
<div class="callout-title-container flex-fill">
Note
</div>
</div>
<div class="callout-body-container callout-body">
<p>Using set operations is more effective when:</p>
<ul>
<li><p>You want to find the first (or last) TRUE</p></li>
<li><p>You have very few TRUE s and very many FALSE s; a set representation may be faster and require less storage.</p></li>
</ul>
</div>
</div>
<p><strong>Why</strong>:</p>
<p>Sets or indices allow direct access to specific positions, making it faster to locate the first or last TRUE value. Operations designed to find the first TRUE can exit early once the condition is met, avoiding unnecessary computations.</p>
<p><em>note:</em>: which() allows you to convert a Boolean representation to an integer representation</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb97"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb97-1"><a href="#cb97-1" aria-hidden="true" tabindex="-1"></a>(x1 <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span> <span class="sc">%%</span> <span class="dv">2</span> <span class="sc">==</span> <span class="dv">0</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb99"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb99-1"><a href="#cb99-1" aria-hidden="true" tabindex="-1"></a>(x2 <span class="ot"><-</span> <span class="fu">which</span>(x1))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 4 6 8 10</code></pre>
</div>
<div class="sourceCode cell-code" id="cb101"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb101-1"><a href="#cb101-1" aria-hidden="true" tabindex="-1"></a>(y1 <span class="ot"><-</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">10</span> <span class="sc">%%</span> <span class="dv">5</span> <span class="sc">==</span> <span class="dv">0</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb103"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb103-1"><a href="#cb103-1" aria-hidden="true" tabindex="-1"></a>(y2 <span class="ot"><-</span> <span class="fu">which</span>(y1))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 5 10</code></pre>
</div>
<div class="sourceCode cell-code" id="cb105"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb105-1"><a href="#cb105-1" aria-hidden="true" tabindex="-1"></a>x1 <span class="sc">&</span> y1</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb107"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb107-1"><a href="#cb107-1" aria-hidden="true" tabindex="-1"></a><span class="fu">intersect</span>(x2, y2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 10</code></pre>
</div>
<div class="sourceCode cell-code" id="cb109"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb109-1"><a href="#cb109-1" aria-hidden="true" tabindex="-1"></a>x1 <span class="sc">|</span> y1</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE TRUE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb111"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb111-1"><a href="#cb111-1" aria-hidden="true" tabindex="-1"></a><span class="fu">union</span>(x2, y2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 4 6 8 10 5</code></pre>
</div>
<div class="sourceCode cell-code" id="cb113"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb113-1"><a href="#cb113-1" aria-hidden="true" tabindex="-1"></a>x1 <span class="sc">&</span> <span class="sc">!</span>y1</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE TRUE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb115"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb115-1"><a href="#cb115-1" aria-hidden="true" tabindex="-1"></a><span class="fu">setdiff</span>(x2, y2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 4 6 8</code></pre>
</div>
<div class="sourceCode cell-code" id="cb117"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb117-1"><a href="#cb117-1" aria-hidden="true" tabindex="-1"></a><span class="fu">xor</span>(x1, y1)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> [1] FALSE TRUE FALSE TRUE TRUE TRUE FALSE TRUE FALSE FALSE</code></pre>
</div>
<div class="sourceCode cell-code" id="cb119"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb119-1"><a href="#cb119-1" aria-hidden="true" tabindex="-1"></a><span class="fu">setdiff</span>(<span class="fu">union</span>(x2, y2), <span class="fu">intersect</span>(x2, y2))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 2 4 6 8 5</code></pre>
</div>
</div>
<p>Common mistake is to use x[which(y)] instead of x[y] -result is exactly the same but 2 differences:</p>
<ul>
<li><p>When the logical vector contains NA , logical subsetting replaces these values with NA while which() drops these values</p></li>
<li><p>x[-which(y)] is not equivalent to x[!y] : if y is all FALSE, which(y) will be integer(0) and -integer(0) is still integer(0) , so you’ll get no values, instead of all values.</p></li>
</ul>
<p>In general, avoid switching from logical to integer subsetting unless you want, for example, the first or last TRUE value.</p>
<p><strong>More Examples to compare performance</strong>:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb121"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb121-1"><a href="#cb121-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Large logical vector</span></span>
<span id="cb121-2"><a href="#cb121-2" aria-hidden="true" tabindex="-1"></a>vec <span class="ot"><-</span> <span class="fu">sample</span>(<span class="fu">c</span>(<span class="cn">TRUE</span>, <span class="cn">FALSE</span>), <span class="at">size =</span> <span class="fl">1e6</span>, <span class="at">replace =</span> <span class="cn">TRUE</span>)</span>
<span id="cb121-3"><a href="#cb121-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb121-4"><a href="#cb121-4" aria-hidden="true" tabindex="-1"></a><span class="co"># integer subsetting to find the first TRUE</span></span>
<span id="cb121-5"><a href="#cb121-5" aria-hidden="true" tabindex="-1"></a>first_true_base <span class="ot"><-</span> <span class="fu">which</span>(vec)[<span class="dv">1</span>]</span>
<span id="cb121-6"><a href="#cb121-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb121-7"><a href="#cb121-7" aria-hidden="true" tabindex="-1"></a><span class="co"># logical subsetting</span></span>
<span id="cb121-8"><a href="#cb121-8" aria-hidden="true" tabindex="-1"></a>first_true_set <span class="ot"><-</span> <span class="fu">match</span>(<span class="cn">TRUE</span>, vec)</span>
<span id="cb121-9"><a href="#cb121-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb121-10"><a href="#cb121-10" aria-hidden="true" tabindex="-1"></a><span class="co">#match(TRUE, vec) can be faster because it stops as soon as it finds the first TRUE, whereas which(vec)[1] computes all TRUE indices first and then selects the first one.</span></span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>When a dataset has very few TRUE values (sparse data) and many FALSE values, using set representations can be more efficient both in terms of speed and memory. Indices represent the positions of TRUE values, saving memory by not storing the FALSE values. Operations on sets typically faster because they involve fewer elements.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb122"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb122-1"><a href="#cb122-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Sparse logical vector</span></span>
<span id="cb122-2"><a href="#cb122-2" aria-hidden="true" tabindex="-1"></a>vec <span class="ot"><-</span> <span class="fu">rep</span>(<span class="cn">FALSE</span>, <span class="fl">1e6</span>)</span>
<span id="cb122-3"><a href="#cb122-3" aria-hidden="true" tabindex="-1"></a>vec[<span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span><span class="fl">1e6</span>, <span class="dv">10</span>)] <span class="ot"><-</span> <span class="cn">TRUE</span> <span class="co"># Only 10 TRUE values</span></span>
<span id="cb122-4"><a href="#cb122-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb122-5"><a href="#cb122-5" aria-hidden="true" tabindex="-1"></a><span class="co"># logical</span></span>
<span id="cb122-6"><a href="#cb122-6" aria-hidden="true" tabindex="-1"></a>subset_base <span class="ot"><-</span> vec[vec <span class="sc">==</span> <span class="cn">TRUE</span>]</span>
<span id="cb122-7"><a href="#cb122-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb122-8"><a href="#cb122-8" aria-hidden="true" tabindex="-1"></a><span class="co"># integer</span></span>
<span id="cb122-9"><a href="#cb122-9" aria-hidden="true" tabindex="-1"></a>true_indices <span class="ot"><-</span> <span class="fu">which</span>(vec)</span>
<span id="cb122-10"><a href="#cb122-10" aria-hidden="true" tabindex="-1"></a>subset_set <span class="ot"><-</span> vec[true_indices]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<div class="cell">
<div class="sourceCode cell-code" id="cb123"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb123-1"><a href="#cb123-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(data.table)</span>
<span id="cb123-2"><a href="#cb123-2" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tibble)</span>
<span id="cb123-3"><a href="#cb123-3" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(collapse)</span>
<span id="cb123-4"><a href="#cb123-4" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(microbenchmark)</span>
<span id="cb123-5"><a href="#cb123-5" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(highcharter)</span>
<span id="cb123-6"><a href="#cb123-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-7"><a href="#cb123-7" aria-hidden="true" tabindex="-1"></a><span class="co"># Set the size for the test data</span></span>
<span id="cb123-8"><a href="#cb123-8" aria-hidden="true" tabindex="-1"></a>size <span class="ot"><-</span> <span class="fl">1e5</span></span>
<span id="cb123-9"><a href="#cb123-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-10"><a href="#cb123-10" aria-hidden="true" tabindex="-1"></a><span class="co"># Create a data frame and convert it to data.table and tibble</span></span>
<span id="cb123-11"><a href="#cb123-11" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(</span>
<span id="cb123-12"><a href="#cb123-12" aria-hidden="true" tabindex="-1"></a> <span class="at">x =</span> <span class="fu">runif</span>(size),</span>
<span id="cb123-13"><a href="#cb123-13" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">runif</span>(size),</span>
<span id="cb123-14"><a href="#cb123-14" aria-hidden="true" tabindex="-1"></a> <span class="at">id =</span> <span class="fu">sample</span>(letters, size, <span class="at">replace =</span> <span class="cn">TRUE</span>)</span>
<span id="cb123-15"><a href="#cb123-15" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb123-16"><a href="#cb123-16" aria-hidden="true" tabindex="-1"></a>dt <span class="ot"><-</span> <span class="fu">as.data.table</span>(df)</span>
<span id="cb123-17"><a href="#cb123-17" aria-hidden="true" tabindex="-1"></a>tb <span class="ot"><-</span> <span class="fu">as_tibble</span>(df)</span>
<span id="cb123-18"><a href="#cb123-18" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-19"><a href="#cb123-19" aria-hidden="true" tabindex="-1"></a><span class="co"># Logical vector with sparse TRUE values</span></span>
<span id="cb123-20"><a href="#cb123-20" aria-hidden="true" tabindex="-1"></a>logical_vector <span class="ot"><-</span> <span class="fu">rep</span>(<span class="cn">FALSE</span>, size)</span>
<span id="cb123-21"><a href="#cb123-21" aria-hidden="true" tabindex="-1"></a>logical_vector[<span class="fu">sample</span>(<span class="dv">1</span><span class="sc">:</span>size, <span class="dv">100</span>)] <span class="ot"><-</span> <span class="cn">TRUE</span> <span class="co"># Only 100 TRUE values</span></span>
<span id="cb123-22"><a href="#cb123-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-23"><a href="#cb123-23" aria-hidden="true" tabindex="-1"></a><span class="co"># Benchmarking logical vs integer subsetting</span></span>
<span id="cb123-24"><a href="#cb123-24" aria-hidden="true" tabindex="-1"></a>bench <span class="ot"><-</span> microbenchmark<span class="sc">::</span><span class="fu">microbenchmark</span>(</span>
<span id="cb123-25"><a href="#cb123-25" aria-hidden="true" tabindex="-1"></a> <span class="at">times =</span> <span class="dv">100</span>,</span>
<span id="cb123-26"><a href="#cb123-26" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-27"><a href="#cb123-27" aria-hidden="true" tabindex="-1"></a> <span class="co"># Logical subsetting for data.frame</span></span>
<span id="cb123-28"><a href="#cb123-28" aria-hidden="true" tabindex="-1"></a> <span class="at">df_logical =</span> {</span>
<span id="cb123-29"><a href="#cb123-29" aria-hidden="true" tabindex="-1"></a> df[logical_vector, ]</span>
<span id="cb123-30"><a href="#cb123-30" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb123-31"><a href="#cb123-31" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-32"><a href="#cb123-32" aria-hidden="true" tabindex="-1"></a> <span class="co"># Integer subsetting for data.frame</span></span>
<span id="cb123-33"><a href="#cb123-33" aria-hidden="true" tabindex="-1"></a> <span class="at">df_integer =</span> {</span>
<span id="cb123-34"><a href="#cb123-34" aria-hidden="true" tabindex="-1"></a> df[<span class="fu">which</span>(logical_vector), ]</span>
<span id="cb123-35"><a href="#cb123-35" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb123-36"><a href="#cb123-36" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-37"><a href="#cb123-37" aria-hidden="true" tabindex="-1"></a> <span class="co"># Logical subsetting for data.table</span></span>
<span id="cb123-38"><a href="#cb123-38" aria-hidden="true" tabindex="-1"></a> <span class="at">dt_logical =</span> {</span>
<span id="cb123-39"><a href="#cb123-39" aria-hidden="true" tabindex="-1"></a> dt[logical_vector]</span>
<span id="cb123-40"><a href="#cb123-40" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb123-41"><a href="#cb123-41" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-42"><a href="#cb123-42" aria-hidden="true" tabindex="-1"></a> <span class="co"># Integer subsetting for data.table</span></span>
<span id="cb123-43"><a href="#cb123-43" aria-hidden="true" tabindex="-1"></a> <span class="at">dt_integer =</span> {</span>
<span id="cb123-44"><a href="#cb123-44" aria-hidden="true" tabindex="-1"></a> dt[<span class="fu">which</span>(logical_vector)]</span>
<span id="cb123-45"><a href="#cb123-45" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb123-46"><a href="#cb123-46" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-47"><a href="#cb123-47" aria-hidden="true" tabindex="-1"></a> <span class="co"># Logical subsetting for tibble</span></span>
<span id="cb123-48"><a href="#cb123-48" aria-hidden="true" tabindex="-1"></a> <span class="at">tb_logical =</span> {</span>
<span id="cb123-49"><a href="#cb123-49" aria-hidden="true" tabindex="-1"></a> tb[logical_vector, ]</span>
<span id="cb123-50"><a href="#cb123-50" aria-hidden="true" tabindex="-1"></a> },</span>
<span id="cb123-51"><a href="#cb123-51" aria-hidden="true" tabindex="-1"></a> </span>
<span id="cb123-52"><a href="#cb123-52" aria-hidden="true" tabindex="-1"></a> <span class="co"># Integer subsetting for tibble</span></span>
<span id="cb123-53"><a href="#cb123-53" aria-hidden="true" tabindex="-1"></a> <span class="at">tb_integer =</span> {</span>
<span id="cb123-54"><a href="#cb123-54" aria-hidden="true" tabindex="-1"></a> tb[<span class="fu">which</span>(logical_vector), ]</span>
<span id="cb123-55"><a href="#cb123-55" aria-hidden="true" tabindex="-1"></a> }</span>
<span id="cb123-56"><a href="#cb123-56" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb123-57"><a href="#cb123-57" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-58"><a href="#cb123-58" aria-hidden="true" tabindex="-1"></a><span class="co"># Visualization using highcharter</span></span>
<span id="cb123-59"><a href="#cb123-59" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> (<span class="fu">requireNamespace</span>(<span class="st">"highcharter"</span>, <span class="at">quietly =</span> <span class="cn">TRUE</span>)) {</span>
<span id="cb123-60"><a href="#cb123-60" aria-hidden="true" tabindex="-1"></a> hc_dt <span class="ot"><-</span> highcharter<span class="sc">::</span><span class="fu">data_to_boxplot</span>(bench,</span>
<span id="cb123-61"><a href="#cb123-61" aria-hidden="true" tabindex="-1"></a> time,</span>
<span id="cb123-62"><a href="#cb123-62" aria-hidden="true" tabindex="-1"></a> expr,</span>
<span id="cb123-63"><a href="#cb123-63" aria-hidden="true" tabindex="-1"></a> <span class="at">add_outliers =</span> <span class="cn">FALSE</span>,</span>
<span id="cb123-64"><a href="#cb123-64" aria-hidden="true" tabindex="-1"></a> <span class="at">name =</span> <span class="st">"Time in milliseconds"</span>)</span>
<span id="cb123-65"><a href="#cb123-65" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb123-66"><a href="#cb123-66" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">highchart</span>() <span class="sc">|></span></span>
<span id="cb123-67"><a href="#cb123-67" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">hc_xAxis</span>(<span class="at">type =</span> <span class="st">"category"</span>) <span class="sc">|></span></span>
<span id="cb123-68"><a href="#cb123-68" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">hc_chart</span>(<span class="at">inverted =</span> <span class="cn">TRUE</span>) <span class="sc">|></span></span>
<span id="cb123-69"><a href="#cb123-69" aria-hidden="true" tabindex="-1"></a> highcharter<span class="sc">::</span><span class="fu">hc_add_series_list</span>(hc_dt)</span>
<span id="cb123-70"><a href="#cb123-70" aria-hidden="true" tabindex="-1"></a>} <span class="cf">else</span> {</span>
<span id="cb123-71"><a href="#cb123-71" aria-hidden="true" tabindex="-1"></a> <span class="fu">boxplot</span>(bench, <span class="at">outline =</span> <span class="cn">FALSE</span>, <span class="at">main =</span> <span class="st">"Logical vs Integer Subsetting Performance"</span>, <span class="at">ylab =</span> <span class="st">"Time (milliseconds)"</span>, <span class="at">las =</span> <span class="dv">2</span>)</span>
<span id="cb123-72"><a href="#cb123-72" aria-hidden="true" tabindex="-1"></a>}</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<div class="highchart html-widget html-fill-item" id="htmlwidget-30d261e8a7ca44ff257b" style="width:100%;height:464px;"></div>
<script type="application/json" data-for="htmlwidget-30d261e8a7ca44ff257b">{"x":{"hc_opts":{"chart":{"reflow":true,"inverted":true},"title":{"text":null},"yAxis":{"title":{"text":null}},"credits":{"enabled":false},"exporting":{"enabled":false},"boost":{"enabled":false},"plotOptions":{"series":{"label":{"enabled":false},"turboThreshold":0},"treemap":{"layoutAlgorithm":"squarified"}},"xAxis":{"type":"category"},"series":[{"name":"Time in milliseconds","data":[{"name":"df_logical","low":109200,"q1":117900,"median":127200,"q3":197550,"high":313600},{"name":"df_integer","low":40400,"q1":47400,"median":54100,"q3":76350,"high":113300},{"name":"dt_logical","low":65400,"q1":73750,"median":84200,"q3":113900,"high":173200},{"name":"dt_integer","low":78800,"q1":90400,"median":109050,"q3":157700,"high":217900},{"name":"tb_logical","low":227300,"q1":233450,"median":247100,"q3":283900,"high":358600},{"name":"tb_integer","low":44500,"q1":50400,"median":56050,"q3":73600,"high":99900}],"id":null,"type":"boxplot"}]},"theme":{"chart":{"backgroundColor":"transparent"},"colors":["#7cb5ec","#434348","#90ed7d","#f7a35c","#8085e9","#f15c80","#e4d354","#2b908f","#f45b5b","#91e8e1"]},"conf_opts":{"global":{"Date":null,"VMLRadialGradientURL":"http =//code.highcharts.com/list(version)/gfx/vml-radial-gradient.png","canvasToolsURL":"http =//code.highcharts.com/list(version)/modules/canvas-tools.js","getTimezoneOffset":null,"timezoneOffset":0,"useUTC":true},"lang":{"contextButtonTitle":"Chart context menu","decimalPoint":".","downloadCSV":"Download CSV","downloadJPEG":"Download JPEG image","downloadPDF":"Download PDF document","downloadPNG":"Download PNG image","downloadSVG":"Download SVG vector image","downloadXLS":"Download XLS","drillUpText":"◁ Back to {series.name}","exitFullscreen":"Exit from full screen","exportData":{"annotationHeader":"Annotations","categoryDatetimeHeader":"DateTime","categoryHeader":"Category"},"hideData":"Hide data table","invalidDate":null,"loading":"Loading...","months":["January","February","March","April","May","June","July","August","September","October","November","December"],"noData":"No data to display","numericSymbolMagnitude":1000,"numericSymbols":["k","M","G","T","P","E"],"printChart":"Print chart","resetZoom":"Reset zoom","resetZoomTitle":"Reset zoom level 1:1","shortMonths":["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"],"shortWeekdays":["Sat","Sun","Mon","Tue","Wed","Thu","Fri"],"thousandsSep":" ","viewData":"View data table","viewFullscreen":"View in full screen","weekdays":["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]}},"type":"chart","fonts":[],"debug":false},"evals":[],"jsHooks":[]}</script>
</div>
</div>
</section>
</section>
<section id="subsetting---some-applications" class="level3" data-number="1.6">
<h3 data-number="1.6" class="anchored" data-anchor-id="subsetting---some-applications"><span class="header-section-number">1.6</span> Subsetting - Some applications</h3>
<section id="character-subsetting" class="level4" data-number="1.6.1">
<h4 data-number="1.6.1" class="anchored" data-anchor-id="character-subsetting"><span class="header-section-number">1.6.1</span> Character subsetting</h4>
<section id="lookup-tables" class="level5" data-number="1.6.1.1">
<h5 data-number="1.6.1.1" class="anchored" data-anchor-id="lookup-tables"><span class="header-section-number">1.6.1.1</span> Lookup tables</h5>
<p>These are usually named vectors or lists, useful when you want to map values from one object to another. For example,</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb124"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb124-1"><a href="#cb124-1" aria-hidden="true" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">c</span>(<span class="st">"m"</span>, <span class="st">"f"</span>, <span class="st">"u"</span>, <span class="st">"f"</span>, <span class="st">"f"</span>, <span class="st">"m"</span>, <span class="st">"m"</span>) </span>
<span id="cb124-2"><a href="#cb124-2" aria-hidden="true" tabindex="-1"></a>lookup <span class="ot"><-</span> <span class="fu">c</span>(<span class="at">m =</span> <span class="st">"Male"</span>, <span class="at">f =</span> <span class="st">"Female"</span>, <span class="at">u =</span> <span class="cn">NA</span>)</span>
<span id="cb124-3"><a href="#cb124-3" aria-hidden="true" tabindex="-1"></a>lookup[x]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> m f u f f m m
"Male" "Female" NA "Female" "Female" "Male" "Male" </code></pre>
</div>
<div class="sourceCode cell-code" id="cb126"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb126-1"><a href="#cb126-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Can use unname() if you don't want names in the result </span></span>
<span id="cb126-2"><a href="#cb126-2" aria-hidden="true" tabindex="-1"></a><span class="fu">unname</span>(lookup[x])</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] "Male" "Female" NA "Female" "Female" "Male" "Male" </code></pre>
</div>
</div>
</section>
<section id="removing-columns-from-data-frames" class="level5" data-number="1.6.1.2">
<h5 data-number="1.6.1.2" class="anchored" data-anchor-id="removing-columns-from-data-frames"><span class="header-section-number">1.6.1.2</span> Removing columns from data frames</h5>
<p>There are two ways of removing columns:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb128"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb128-1"><a href="#cb128-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Option 1 - Set it to NULL</span></span>
<span id="cb128-2"><a href="#cb128-2" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">y =</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">z =</span> letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>])</span>
<span id="cb128-3"><a href="#cb128-3" aria-hidden="true" tabindex="-1"></a>df<span class="sc">$</span>z <span class="ot"><-</span> <span class="cn">NULL</span></span>
<span id="cb128-4"><a href="#cb128-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb128-5"><a href="#cb128-5" aria-hidden="true" tabindex="-1"></a><span class="co"># Option 2 - Subset the columns you want </span></span>
<span id="cb128-6"><a href="#cb128-6" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>, <span class="at">y =</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">z =</span> letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>])</span>
<span id="cb128-7"><a href="#cb128-7" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">c</span>(<span class="st">"x"</span>, <span class="st">"y"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> x y
1 1 3
2 2 2
3 3 1</code></pre>
</div>
</div>
<p>If you only know the columns you do not want (e.g., “z”):</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb130"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb130-1"><a href="#cb130-1" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">setdiff</span>(<span class="fu">names</span>(df), <span class="st">"z"</span>)]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> x y
1 1 3
2 2 2
3 3 1</code></pre>
</div>
</div>
</section>
</section>
<section id="integer-subsetting" class="level4" data-number="1.6.2">
<h4 data-number="1.6.2" class="anchored" data-anchor-id="integer-subsetting"><span class="header-section-number">1.6.2</span> Integer subsetting</h4>
<section id="lookup-tables---matching-by-hand" class="level5" data-number="1.6.2.1">
<h5 data-number="1.6.2.1" class="anchored" data-anchor-id="lookup-tables---matching-by-hand"><span class="header-section-number">1.6.2.1</span> Lookup tables - matching by hand</h5>
<p>Suppose you have a more complex lookup table, i.e., with multiple columns of information.</p>
<p>For example, suppose you have a vector of integer grades and a table that describes their property:</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb132"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb132-1"><a href="#cb132-1" aria-hidden="true" tabindex="-1"></a>grades <span class="ot"><-</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">1</span>) </span>
<span id="cb132-2"><a href="#cb132-2" aria-hidden="true" tabindex="-1"></a>info <span class="ot"><-</span> <span class="fu">data.frame</span>(</span>
<span id="cb132-3"><a href="#cb132-3" aria-hidden="true" tabindex="-1"></a> <span class="at">grade =</span> <span class="dv">3</span><span class="sc">:</span><span class="dv">1</span>,</span>
<span id="cb132-4"><a href="#cb132-4" aria-hidden="true" tabindex="-1"></a> <span class="at">desc =</span> <span class="fu">c</span>(<span class="st">"Excellent"</span>, <span class="st">"Good"</span>, <span class="st">"Poor"</span>),</span>
<span id="cb132-5"><a href="#cb132-5" aria-hidden="true" tabindex="-1"></a> <span class="at">fail =</span> <span class="fu">c</span>(F, F, T)</span>
<span id="cb132-6"><a href="#cb132-6" aria-hidden="true" tabindex="-1"></a>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>say we want to have a row for each value in grades. A way to do this is by combining match() and integer subsetting</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb133"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb133-1"><a href="#cb133-1" aria-hidden="true" tabindex="-1"></a>id <span class="ot"><-</span> <span class="fu">match</span>(grades, info<span class="sc">$</span>grade) </span>
<span id="cb133-2"><a href="#cb133-2" aria-hidden="true" tabindex="-1"></a><span class="co">#return position where each grade is found in info$grade</span></span>
<span id="cb133-3"><a href="#cb133-3" aria-hidden="true" tabindex="-1"></a>id</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 3 2 2 1 3</code></pre>
</div>
<div class="sourceCode cell-code" id="cb135"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb135-1"><a href="#cb135-1" aria-hidden="true" tabindex="-1"></a>info[id, ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code> grade desc fail
3 1 Poor TRUE
2 2 Good FALSE
2.1 2 Good FALSE
1 3 Excellent FALSE
3.1 1 Poor TRUE</code></pre>
</div>
</div>
</section>
<section id="random-samples" class="level5" data-number="1.6.2.2">
<h5 data-number="1.6.2.2" class="anchored" data-anchor-id="random-samples"><span class="header-section-number">1.6.2.2</span> Random samples</h5>
<p>You can use integer indices to randomly sample a vector or data frame.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb137"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb137-1"><a href="#cb137-1" aria-hidden="true" tabindex="-1"></a><span class="co"># use sample(n) to generate a random permutation of 1:n </span></span>
<span id="cb137-2"><a href="#cb137-2" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb137-3"><a href="#cb137-3" aria-hidden="true" tabindex="-1"></a>df <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">x =</span> <span class="fu">c</span>(<span class="dv">1</span>, <span class="dv">2</span>, <span class="dv">3</span>, <span class="dv">1</span>, <span class="dv">2</span>), <span class="at">y =</span> <span class="dv">5</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">z =</span> letters[<span class="dv">1</span><span class="sc">:</span><span class="dv">5</span>])</span>
<span id="cb137-4"><a href="#cb137-4" aria-hidden="true" tabindex="-1"></a><span class="co"># Randomly reorder</span></span>
<span id="cb137-5"><a href="#cb137-5" aria-hidden="true" tabindex="-1"></a>df[<span class="fu">sample</span>(<span class="fu">nrow</span>(df)), ]</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>