-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotes_vs_backticks.html
More file actions
894 lines (817 loc) · 33.5 KB
/
quotes_vs_backticks.html
File metadata and controls
894 lines (817 loc) · 33.5 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Quotes vs backticks</title>
<script src="site_libs/header-attrs-2.14/header-attrs.js"></script>
<script src="site_libs/jquery-3.6.0/jquery-3.6.0.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/spacelab.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<script src="site_libs/kePrint-0.0.1/kePrint.js"></script>
<link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet" />
<link rel="shortcut icon" href="resources/images/favicon.ico" />
<!--- go to https://favicon.io/favicon-converter/ to upload an image to make a new favicon.io. You will need to replace the current favicon.io image with the one in the downloaded directory from the website. The current image is in the resources/images/ directory --->
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<link rel="stylesheet" href="docs/web_styles.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
details > summary > p:only-child {
display: inline;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
</head>
<body>
<div class="container-fluid main-container">
<div class="navbar navbar-default navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-bs-toggle="collapse" data-target="#navbar" data-bs-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Intro to R</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="materials_schedule.html">Materials + Schedule</a>
</li>
<li>
<a href="resources.html">Resources</a>
</li>
<li>
<a href="help.html">Help</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Quotes vs backticks</h1>
</div>
<div id="TOC">
<ul>
<li><a href="#values-in-general">Values in general</a></li>
<li><a href="#column-names">Column names</a></li>
<li><a href="#tibble-for-naming-variables"><code>tibble()</code> for naming variables</a></li>
<li><a href="#rename"><code>rename()</code></a></li>
<li><a href="#summarize"><code>summarize()</code></a></li>
<li><a href="#mutate"><code>mutate()</code></a></li>
<li><a href="#readr-expects-quotes-not-backticks"><code>readr</code> expects quotes not backticks</a></li>
<li><a href="#tidyselect-helper-functions-like-contains">tidyselect helper functions like <code>contains()</code></a></li>
<li><a href="#recode"><code>recode()</code></a></li>
<li><a href="#case_when"><code>case_when()</code></a></li>
<li><a href="#stringr-functions"><code>stringr</code> functions</a></li>
<li><a href="#pivot_longer-and-pivot_wider"><code>pivot_longer</code> and <code>pivot_wider</code></a></li>
<li><a href="#dont-need-quotes-for-modeling-with-glm">Don’t need quotes for modeling with <code>glm()</code></a></li>
</ul>
</div>
<p>When do you use quotes, backticks, or nothing in R?</p>
<p>There are some inconsistencies across different functions, sometimes you just need to try them out. Always check that your work did what you expect!</p>
<div id="values-in-general" class="section level2">
<h2>Values in general</h2>
<p>In general these are the conventions for working with values (recall that values or samples within rows):</p>
<table class=" lightable-paper" style="font-size: 18px; font-family: helvetica; width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;text-align: center;">
Type
</th>
<th style="text-align:left;text-align: center;">
Example
</th>
<th style="text-align:left;text-align: center;">
Features
</th>
<th style="text-align:left;text-align: center;">
Use
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
character string
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
Liver
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
word
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
use quotes - single or double
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
character string
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
New York
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
phrase
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
use quotes - single or double
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
atypical
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
col 1
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
has spaces and numbers
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
use <code>backticks</code>
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
atypical
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
col.1
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
has punctuation
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
use <code>backticks</code>
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
atypical
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
1
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
just a number
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
use <code>backticks</code>
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
atypical
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
1st col
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
starts with number
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
use <code>backticks</code>
</td>
</tr>
</tbody>
</table>
</div>
<div id="column-names" class="section level2">
<h2>Column names</h2>
<p>Column names are slightly different. We will go over some of these situations in more detail, but here is a summary:</p>
<table class=" lightable-paper" style="font-size: 18px; font-family: helvetica; width: auto !important; margin-left: auto; margin-right: auto;">
<thead>
<tr>
<th style="text-align:left;text-align: center;">
Function
</th>
<th style="text-align:left;text-align: center;">
Use
</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
tibble()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
Tolerates quotes or backticks if atypical
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
rename()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
Tolerates quotes or backticks if atypical
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
filter()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
Only backticks if atypical
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
summarize()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
Only backticks if atypical
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
mutate()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
Tolerates quotes or backticks if atypical
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
readr()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
Only quotes tolearted
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
tidyselect functions (ex.starts_with())
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
Only quotes tolerated
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
recode()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightgreen !important;">
Tolerates quotes or backticks if atypical
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
case_when()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
Only quotes tolerated
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
stringr functions (ex. str_detect())
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
Only quotes tolerated
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
pivot_longer()/pivot_wider()
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: lightblue !important;">
Only quotes tolerated
</td>
</tr>
<tr>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
modeling functions
</td>
<td style="text-align:left;font-weight: bold;border-left:1px solid;border-right:1px solid;background-color: yellow !important;">
Only backticks if atypical
</td>
</tr>
</tbody>
</table>
</div>
<div id="tibble-for-naming-variables" class="section level2">
<h2><code>tibble()</code> for naming variables</h2>
<ul>
<li><p>we suggest that you avoid nonstandard <strong>variable</strong> names - if you do you do not need any quotes or backticks around the name within <code>tibble</code></p></li>
<li><p>backticks are typically for nonstandard <strong>variable</strong> names (aka column names):</p>
<ul>
<li>those with spaces <code>name with space</code></li>
<li>those with punctuation <code>name!</code></li>
<li>those that are just numbers <code>12</code></li>
<li>those that start with numbers <code>1name</code></li>
</ul></li>
<li><p>single or double quotes are typically used for <strong>character strings</strong> for the values within the data</p></li>
</ul>
<p>For example, in the <code>iris</code> data set:</p>
<pre class="r"><code>head(iris)</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa</code></pre>
<p>Sepal.Length is a column/variable name that would often need backticks, while the Species setosa is character string value for the variable Species, and it would need quotes.</p>
<p>Let’s check out how some functions work with this.</p>
<p>In the <code>tibble()</code> function when specifying names we need to use backticks when we have spaces or punctuation or variable names that are just numeric characters (this also works with single quotes or double quotes, but backticks are more common practice).</p>
<p>This is not required if we have a typical type of name without spaces or punctuation. However if we do use quotes on such a name is is not a problem either.</p>
<pre class="r"><code># the preferred method
ex_data <- tibble(
`Number!` = seq(from = 1, to = 5),
`Var with space` = c("A", "B", "C", "D", "E"),
`2022` = sample(seq(from = 1, to = 5), size = 5),
`2021` = sample(seq(from = 1, to = 5), size = 5),
typical = seq(from = 1, to = 5),
`typical2` = seq(from = 1, to = 5)
)
ex_data</code></pre>
<pre><code>## # A tibble: 5 × 6
## `Number!` `Var with space` `2022` `2021` typical typical2
## <int> <chr> <int> <int> <int> <int>
## 1 1 A 1 3 1 1
## 2 2 B 2 2 2 2
## 3 3 C 3 1 3 3
## 4 4 D 4 5 4 4
## 5 5 E 5 4 5 5</code></pre>
<pre class="r"><code># this works
ex_data <- tibble(
"Number!" = seq(from = 1, to = 5),
"Var with space" = c("A", "B", "C", "D", "E"),
"2022" = sample(seq(from = 1, to = 5), size = 5),
"2021" = sample(seq(from = 1, to = 5), size = 5),
typical = seq(from = 1, to = 5),
"typical2" = seq(from = 1, to = 5)
)
ex_data</code></pre>
<pre><code>## # A tibble: 5 × 6
## `Number!` `Var with space` `2022` `2021` typical typical2
## <int> <chr> <int> <int> <int> <int>
## 1 1 A 2 3 1 1
## 2 2 B 4 5 2 2
## 3 3 C 3 1 3 3
## 4 4 D 5 2 4 4
## 5 5 E 1 4 5 5</code></pre>
</div>
<div id="rename" class="section level2">
<h2><code>rename()</code></h2>
<p>same thing here…</p>
<pre class="r"><code>ex_data %>%
rename(Number = `Number!`)</code></pre>
<pre><code>## # A tibble: 5 × 6
## Number `Var with space` `2022` `2021` typical typical2
## <int> <chr> <int> <int> <int> <int>
## 1 1 A 2 3 1 1
## 2 2 B 4 5 2 2
## 3 3 C 3 1 3 3
## 4 4 D 5 2 4 4
## 5 5 E 1 4 5 5</code></pre>
<pre class="r"><code>ex_data %>%
rename(Number = "Number!")</code></pre>
<pre><code>## # A tibble: 5 × 6
## Number `Var with space` `2022` `2021` typical typical2
## <int> <chr> <int> <int> <int> <int>
## 1 1 A 2 3 1 1
## 2 2 B 4 5 2 2
## 3 3 C 3 1 3 3
## 4 4 D 5 2 4 4
## 5 5 E 1 4 5 5</code></pre>
<pre class="r"><code># not necessary but not problematic to put new typical name in quotes
ex_data %>%
rename("Number" = "Number!")</code></pre>
<pre><code>## # A tibble: 5 × 6
## Number `Var with space` `2022` `2021` typical typical2
## <int> <chr> <int> <int> <int> <int>
## 1 1 A 2 3 1 1
## 2 2 B 4 5 2 2
## 3 3 C 3 1 3 3
## 4 4 D 5 2 4 4
## 5 5 E 1 4 5 5</code></pre>
<p>Here is another example…</p>
<pre class="r"><code># This works because Sepal.Length already exists as a column name. However `new name!` needs backticks because it doesn't exist yet and R needs to know what it is (not a not equal to conditional for example), as R could interpret it differently.
iris %>%
rename(`new name!` = Sepal.Length) %>%
head()</code></pre>
<pre><code>## new name! Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa</code></pre>
<p>##<code>filter()</code> function</p>
<p>It is best to avoid using quotes for column names!</p>
<p>Even though this is an atypical column name, this will not work like you would expect.</p>
<p>Here we see values less than 5 for <code>Sepal.Length</code>.</p>
<pre class="r"><code>filter(iris, "Sepal.Length" > 5) %>% head()</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 4.9 3.0 1.4 0.2 setosa
## 3 4.7 3.2 1.3 0.2 setosa
## 4 4.6 3.1 1.5 0.2 setosa
## 5 5.0 3.6 1.4 0.2 setosa
## 6 5.4 3.9 1.7 0.4 setosa</code></pre>
<p>Instead stick to just using the column name as it is - filter only works on columns that already exists, so it knows what to look for.</p>
<p>This works!</p>
<pre class="r"><code>filter(iris, Sepal.Length > 5) %>% head()</code></pre>
<pre><code>## Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1 5.1 3.5 1.4 0.2 setosa
## 2 5.4 3.9 1.7 0.4 setosa
## 3 5.4 3.7 1.5 0.2 setosa
## 4 5.8 4.0 1.2 0.2 setosa
## 5 5.7 4.4 1.5 0.4 setosa
## 6 5.4 3.9 1.3 0.4 setosa</code></pre>
</div>
<div id="summarize" class="section level2">
<h2><code>summarize()</code></h2>
<p>Only backticks will work here. Otherwise, the variable gets interpreted as a character string if we use quotes.</p>
<pre class="r"><code>ex_data %>%
summarize(mean = mean(`2022`))</code></pre>
<pre><code>## # A tibble: 1 × 1
## mean
## <dbl>
## 1 3</code></pre>
<pre class="r"><code># will not work
# ex_data %>%
# summarize(mean = mean("2022"))
# will not work
# ex_data %>%
# summarize(mean = mean("Number!"))</code></pre>
</div>
<div id="mutate" class="section level2">
<h2><code>mutate()</code></h2>
<p>Only these work…</p>
<pre class="r"><code>ex_data %>% mutate(`Number!` = `Number!` + 2)</code></pre>
<pre><code>## # A tibble: 5 × 6
## `Number!` `Var with space` `2022` `2021` typical typical2
## <dbl> <chr> <int> <int> <int> <int>
## 1 3 A 2 3 1 1
## 2 4 B 4 5 2 2
## 3 5 C 3 1 3 3
## 4 6 D 5 2 4 4
## 5 7 E 1 4 5 5</code></pre>
<pre class="r"><code>ex_data %>% mutate("Number!" = `Number!` + 2)</code></pre>
<pre><code>## # A tibble: 5 × 6
## `Number!` `Var with space` `2022` `2021` typical typical2
## <dbl> <chr> <int> <int> <int> <int>
## 1 3 A 2 3 1 1
## 2 4 B 4 5 2 2
## 3 5 C 3 1 3 3
## 4 6 D 5 2 4 4
## 5 7 E 1 4 5 5</code></pre>
<pre class="r"><code>ex_data %>% mutate(Typical = `Number!` + 2)</code></pre>
<pre><code>## # A tibble: 5 × 7
## `Number!` `Var with space` `2022` `2021` typical typical2 Typical
## <int> <chr> <int> <int> <int> <int> <dbl>
## 1 1 A 2 3 1 1 3
## 2 2 B 4 5 2 2 4
## 3 3 C 3 1 3 3 5
## 4 4 D 5 2 4 4 6
## 5 5 E 1 4 5 5 7</code></pre>
</div>
<div id="readr-expects-quotes-not-backticks" class="section level2">
<h2><code>readr</code> expects quotes not backticks</h2>
<p>This is true for URLs or paths.</p>
<pre class="r"><code># Use this
vacc <- read_csv(file = "http://jhudatascience.org/intro_to_r/data/vaccinations.csv")</code></pre>
</div>
<div id="tidyselect-helper-functions-like-contains" class="section level2">
<h2>tidyselect helper functions like <code>contains()</code></h2>
<p>These require quotes.</p>
<pre class="r"><code>select(iris, contains("et")) %>% head(n = 2)</code></pre>
<pre><code>## Petal.Length Petal.Width
## 1 1.4 0.2
## 2 1.4 0.2</code></pre>
<pre class="r"><code>select(iris, ends_with("Width")) %>% head(n = 2)</code></pre>
<pre><code>## Sepal.Width Petal.Width
## 1 3.5 0.2
## 2 3.0 0.2</code></pre>
</div>
<div id="recode" class="section level2">
<h2><code>recode()</code></h2>
<p>Don’t need quotes for existing values.</p>
<pre class="r"><code>ToothGrowth %>%
mutate(supp = recode(supp,
VC = "Ascorbic_Acid",
OJ = "Orange_juice"
)) %>%
count(supp)</code></pre>
<pre><code>## supp n
## 1 Orange_juice 30
## 2 Ascorbic_Acid 30</code></pre>
<p>However it tolerates this.</p>
<pre class="r"><code>ToothGrowth %>%
mutate(supp = recode(supp,
"VC" = "Ascorbic_Acid",
"OJ" = "Orange_juice"
)) %>%
count(supp)</code></pre>
<pre><code>## supp n
## 1 Orange_juice 30
## 2 Ascorbic_Acid 30</code></pre>
<p>Backticks or quotes work for atypical column names.</p>
<pre class="r"><code>ToothGrowth %>%
mutate(supp = paste0(supp, "!")) %>% # making atypical
mutate(supp = recode(supp,
`VC!` = "Ascorbic_Acid",
`OJ!` = "Orange_juice"
)) %>%
count(supp)</code></pre>
<pre><code>## supp n
## 1 Ascorbic_Acid 30
## 2 Orange_juice 30</code></pre>
<pre class="r"><code>ToothGrowth %>%
mutate(supp = paste0(supp, "!")) %>% # making atypical
mutate(supp = recode(supp,
"VC!" = "Ascorbic_Acid",
"OJ!" = "Orange_juice"
)) %>%
count(supp)</code></pre>
<pre><code>## supp n
## 1 Ascorbic_Acid 30
## 2 Orange_juice 30</code></pre>
</div>
<div id="case_when" class="section level2">
<h2><code>case_when()</code></h2>
<p>Need quotes for conditionals and new values.</p>
<pre class="r"><code>ToothGrowth %>%
mutate(supp = case_when(
supp == "VC" ~ "Ascorbic_Acid",
supp == "OJ" ~ "Orange_juice"
)) %>%
count(supp)</code></pre>
<pre><code>## supp n
## 1 Ascorbic_Acid 30
## 2 Orange_juice 30</code></pre>
<p>Only quotes work for atypical values. Backticks do not.</p>
<pre class="r"><code>ToothGrowth %>%
mutate(supp = paste0(supp, "!")) %>% # making atypical
mutate(supp = case_when(
supp == "VC!" ~ "Ascorbic_Acid",
supp == "OJ!" ~ "Orange_juice"
)) %>%
count(supp)</code></pre>
<pre><code>## supp n
## 1 Ascorbic_Acid 30
## 2 Orange_juice 30</code></pre>
</div>
<div id="stringr-functions" class="section level2">
<h2><code>stringr</code> functions</h2>
<p>When working with strings we need to use quotes.</p>
<pre class="r"><code>x <- c("cat", "dog", "mouse")
# this will not work:
# x <- c(`cat`, `dog`, `mouse`)</code></pre>
<p>When looking for patterns we need to use quotes because we are using it as a character string and quotes designate this. Backticks will not work.</p>
<pre class="r"><code>x <- c("cat", "dog", "mouse")
# this will not work:
# x <- c(`cat`, `dog`, `mouse`)
str_detect(pattern = "t", string = x)</code></pre>
<pre><code>## [1] TRUE FALSE FALSE</code></pre>
<pre class="r"><code># this will not work:
# str_detect(pattern = `t`, string = x)</code></pre>
</div>
<div id="pivot_longer-and-pivot_wider" class="section level2">
<h2><code>pivot_longer</code> and <code>pivot_wider</code></h2>
<p>For column names that do not exist yet, you need quotes (single or double). Backticks will not work.</p>
<pre class="r"><code>library(jhur)
wide_circ <- read_circulator()
long_circ <- wide_circ %>%
pivot_longer(
cols = contains(c("boarding")),
names_to = "Route!",
values_to = "Boardings"
)
long_circ <- wide_circ %>%
pivot_longer(
cols = contains(c("boarding")),
names_to = "Route",
values_to = "Boardings"
)</code></pre>
<p>These will not work:</p>
<pre class="r"><code>long_circ <- wide_circ %>%
pivot_longer(
cols = contains(c("boarding")),
names_to = `Route`,
values_to = `Boardings`
)
long_circ <- wide_circ %>%
pivot_longer(
cols = contains(c("boarding")),
names_to = Route,
values_to = Boardings
)</code></pre>
</div>
<div id="dont-need-quotes-for-modeling-with-glm" class="section level2">
<h2>Don’t need quotes for modeling with <code>glm()</code></h2>
<pre class="r"><code>car_data <- mtcars
fit_cars <- glm(mpg ~ cyl + disp + hp + wt * gear, data = car_data)</code></pre>
<p>Use backticks if there is an atypical name. Do not use quotes.</p>
<pre class="r"><code>car_data <- rename(car_data, `mpg!` = mpg)
fit_cars <- glm(`mpg!` ~ cyl + disp + hp + wt * gear, data = car_data)</code></pre>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>