Debian Codegraph @

inst/doc/making-dummy-variables.html

raw
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13 <meta name="author" content="Jacob Kaplan" />
14
15 <meta name="date" content="2026-04-22" />
16
17 <title>Making dummy variables with dummy_cols()</title>
18
19 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
20 // be compatible with the behavior of Pandoc < 2.8).
21 document.addEventListener('DOMContentLoaded', function(e) {
22 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
23 var i, h, a;
24 for (i = 0; i < hs.length; i++) {
25 h = hs[i];
26 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
27 a = h.attributes;
28 while (a.length > 0) h.removeAttribute(a[0].name);
29 }
30 });
31 </script>
32
33 <style type="text/css">
34 code{white-space: pre-wrap;}
35 span.smallcaps{font-variant: small-caps;}
36 span.underline{text-decoration: underline;}
37 div.column{display: inline-block; vertical-align: top; width: 50%;}
38 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
39 ul.task-list{list-style: none;}
40 </style>
41
42
43
44 <style type="text/css">
45 code {
46 white-space: pre;
47 }
48 .sourceCode {
49 overflow: visible;
50 }
51 </style>
52 <style type="text/css" data-origin="pandoc">
53 html { -webkit-text-size-adjust: 100%; }
54 pre > code.sourceCode { white-space: pre; position: relative; }
55 pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
56 pre > code.sourceCode > span:empty { height: 1.2em; }
57 .sourceCode { overflow: visible; }
58 code.sourceCode > span { color: inherit; text-decoration: inherit; }
59 div.sourceCode { margin: 1em 0; }
60 pre.sourceCode { margin: 0; }
61 @media screen {
62 div.sourceCode { overflow: auto; }
63 }
64 @media print {
65 pre > code.sourceCode { white-space: pre-wrap; }
66 pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
67 }
68 pre.numberSource code
69 { counter-reset: source-line 0; }
70 pre.numberSource code > span
71 { position: relative; left: -4em; counter-increment: source-line; }
72 pre.numberSource code > span > a:first-child::before
73 { content: counter(source-line);
74 position: relative; left: -1em; text-align: right; vertical-align: baseline;
75 border: none; display: inline-block;
76 -webkit-touch-callout: none; -webkit-user-select: none;
77 -khtml-user-select: none; -moz-user-select: none;
78 -ms-user-select: none; user-select: none;
79 padding: 0 4px; width: 4em;
80 color: #aaaaaa;
81 }
82 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
83 div.sourceCode
84 { }
85 @media screen {
86 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
87 }
88 code span.al { color: #ff0000; font-weight: bold; }
89 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
90 code span.at { color: #7d9029; }
91 code span.bn { color: #40a070; }
92 code span.bu { color: #008000; }
93 code span.cf { color: #007020; font-weight: bold; }
94 code span.ch { color: #4070a0; }
95 code span.cn { color: #880000; }
96 code span.co { color: #60a0b0; font-style: italic; }
97 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
98 code span.do { color: #ba2121; font-style: italic; }
99 code span.dt { color: #902000; }
100 code span.dv { color: #40a070; }
101 code span.er { color: #ff0000; font-weight: bold; }
102 code span.ex { }
103 code span.fl { color: #40a070; }
104 code span.fu { color: #06287e; }
105 code span.im { color: #008000; font-weight: bold; }
106 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
107 code span.kw { color: #007020; font-weight: bold; }
108 code span.op { color: #666666; }
109 code span.ot { color: #007020; }
110 code span.pp { color: #bc7a00; }
111 code span.sc { color: #4070a0; }
112 code span.ss { color: #bb6688; }
113 code span.st { color: #4070a0; }
114 code span.va { color: #19177c; }
115 code span.vs { color: #4070a0; }
116 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
117 </style>
118 <script>
119 // apply pandoc div.sourceCode style to pre.sourceCode instead
120 (function() {
121 var sheets = document.styleSheets;
122 for (var i = 0; i < sheets.length; i++) {
123 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
124 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
125 var j = 0;
126 while (j < rules.length) {
127 var rule = rules[j];
128 // check if there is a div.sourceCode rule
129 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
130 j++;
131 continue;
132 }
133 var style = rule.style.cssText;
134 // check if color or background-color is set
135 if (rule.style.color === '' && rule.style.backgroundColor === '') {
136 j++;
137 continue;
138 }
139 // replace div.sourceCode by a pre.sourceCode rule
140 sheets[i].deleteRule(j);
141 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
142 }
143 }
144 })();
145 </script>
146
147
148
149
150 <style type="text/css">body {
151 background-color: #fff;
152 margin: 1em auto;
153 max-width: 700px;
154 overflow: visible;
155 padding-left: 2em;
156 padding-right: 2em;
157 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
158 font-size: 14px;
159 line-height: 1.35;
160 }
161 #TOC {
162 clear: both;
163 margin: 0 0 10px 10px;
164 padding: 4px;
165 width: 400px;
166 border: 1px solid #CCCCCC;
167 border-radius: 5px;
168 background-color: #f6f6f6;
169 font-size: 13px;
170 line-height: 1.3;
171 }
172 #TOC .toctitle {
173 font-weight: bold;
174 font-size: 15px;
175 margin-left: 5px;
176 }
177 #TOC ul {
178 padding-left: 40px;
179 margin-left: -1.5em;
180 margin-top: 5px;
181 margin-bottom: 5px;
182 }
183 #TOC ul ul {
184 margin-left: -2em;
185 }
186 #TOC li {
187 line-height: 16px;
188 }
189 table {
190 margin: 1em auto;
191 border-width: 1px;
192 border-color: #DDDDDD;
193 border-style: outset;
194 border-collapse: collapse;
195 }
196 table th {
197 border-width: 2px;
198 padding: 5px;
199 border-style: inset;
200 }
201 table td {
202 border-width: 1px;
203 border-style: inset;
204 line-height: 18px;
205 padding: 5px 5px;
206 }
207 table, table th, table td {
208 border-left-style: none;
209 border-right-style: none;
210 }
211 table thead, table tr.even {
212 background-color: #f7f7f7;
213 }
214 p {
215 margin: 0.5em 0;
216 }
217 blockquote {
218 background-color: #f6f6f6;
219 padding: 0.25em 0.75em;
220 }
221 hr {
222 border-style: solid;
223 border: none;
224 border-top: 1px solid #777;
225 margin: 28px 0;
226 }
227 dl {
228 margin-left: 0;
229 }
230 dl dd {
231 margin-bottom: 13px;
232 margin-left: 13px;
233 }
234 dl dt {
235 font-weight: bold;
236 }
237 ul {
238 margin-top: 0;
239 }
240 ul li {
241 list-style: circle outside;
242 }
243 ul ul {
244 margin-bottom: 0;
245 }
246 pre, code {
247 background-color: #f7f7f7;
248 border-radius: 3px;
249 color: #333;
250 white-space: pre-wrap;
251 }
252 pre {
253 border-radius: 3px;
254 margin: 5px 0px 10px 0px;
255 padding: 10px;
256 }
257 pre:not([class]) {
258 background-color: #f7f7f7;
259 }
260 code {
261 font-family: Consolas, Monaco, 'Courier New', monospace;
262 font-size: 85%;
263 }
264 p > code, li > code {
265 padding: 2px 0px;
266 }
267 div.figure {
268 text-align: center;
269 }
270 img {
271 background-color: #FFFFFF;
272 padding: 2px;
273 border: 1px solid #DDDDDD;
274 border-radius: 3px;
275 border: 1px solid #CCCCCC;
276 margin: 0 5px;
277 }
278 h1 {
279 margin-top: 0;
280 font-size: 35px;
281 line-height: 40px;
282 }
283 h2 {
284 border-bottom: 4px solid #f7f7f7;
285 padding-top: 10px;
286 padding-bottom: 2px;
287 font-size: 145%;
288 }
289 h3 {
290 border-bottom: 2px solid #f7f7f7;
291 padding-top: 10px;
292 font-size: 120%;
293 }
294 h4 {
295 border-bottom: 1px solid #f7f7f7;
296 margin-left: 8px;
297 font-size: 105%;
298 }
299 h5, h6 {
300 border-bottom: 1px solid #ccc;
301 font-size: 105%;
302 }
303 a {
304 color: #0033dd;
305 text-decoration: none;
306 }
307 a:hover {
308 color: #6666ff; }
309 a:visited {
310 color: #800080; }
311 a:visited:hover {
312 color: #BB00BB; }
313 a[href^="http:"] {
314 text-decoration: underline; }
315 a[href^="https:"] {
316 text-decoration: underline; }
317
318 code > span.kw { color: #555; font-weight: bold; }
319 code > span.dt { color: #902000; }
320 code > span.dv { color: #40a070; }
321 code > span.bn { color: #d14; }
322 code > span.fl { color: #d14; }
323 code > span.ch { color: #d14; }
324 code > span.st { color: #d14; }
325 code > span.co { color: #888888; font-style: italic; }
326 code > span.ot { color: #007020; }
327 code > span.al { color: #ff0000; font-weight: bold; }
328 code > span.fu { color: #900; font-weight: bold; }
329 code > span.er { color: #a61717; background-color: #e3d2d2; }
330 </style>
331
332
333
334
335 </head>
336
337 <body>
338
339
340
341
342 <h1 class="title toc-ignore">Making dummy variables with
343 dummy_cols()</h1>
344 <h4 class="author">Jacob Kaplan</h4>
345 <h4 class="date">2026-04-22</h4>
346
347
348
349 <p>Dummy variables (or binary variables) are commonly used in
350 statistical analyses and in more simple descriptive statistics. A dummy
351 column is one which has a value of one when a categorical event occurs
352 and a zero when it doesn’t occur. In most cases this is a feature of the
353 event/person/object being described. For example, if the dummy variable
354 was for occupation being an R programmer, you can ask, “is this person
355 an R programmer?” When the answer is yes, they get a value of 1, when it
356 is no, they get a value of 0.</p>
357 <p>We’ll start with a simple example and then go into using the function
358 <code>dummy_cols()</code>. You can also use the function
359 <code>dummy_columns()</code> which is identical to
360 <code>dummy_cols()</code>.</p>
361 <p>Imagine you have a data set about animals in a local shelter. One of
362 the columns in your data is what animal it is: dog or cat.</p>
363 <table>
364 <thead>
365 <tr class="header">
366 <th align="left">animals</th>
367 </tr>
368 </thead>
369 <tbody>
370 <tr class="odd">
371 <td align="left">dog</td>
372 </tr>
373 <tr class="even">
374 <td align="left">dog</td>
375 </tr>
376 <tr class="odd">
377 <td align="left">cat</td>
378 </tr>
379 </tbody>
380 </table>
381 <p>To make dummy columns from this data, you would need to produce two
382 new columns. One would indicate if the animal is a dog, and the other
383 would indicate if the animal is a cat. Each row would get a value of 1
384 in the column indicating which animal they are, and 0 in the other
385 column.</p>
386 <table>
387 <thead>
388 <tr class="header">
389 <th>animals</th>
390 <th>dog</th>
391 <th>cat</th>
392 </tr>
393 </thead>
394 <tbody>
395 <tr class="odd">
396 <td>dog</td>
397 <td>1</td>
398 <td>0</td>
399 </tr>
400 <tr class="even">
401 <td>dog</td>
402 <td>1</td>
403 <td>0</td>
404 </tr>
405 <tr class="odd">
406 <td>cat</td>
407 <td>0</td>
408 <td>1</td>
409 </tr>
410 </tbody>
411 </table>
412 <p>In the function dummy_cols, the names of these new columns are
413 concatenated to the original column and separated by an underscore.</p>
414 <table>
415 <thead>
416 <tr class="header">
417 <th>animals</th>
418 <th>animals_dog</th>
419 <th>animals_cat</th>
420 </tr>
421 </thead>
422 <tbody>
423 <tr class="odd">
424 <td>dog</td>
425 <td>1</td>
426 <td>0</td>
427 </tr>
428 <tr class="even">
429 <td>dog</td>
430 <td>1</td>
431 <td>0</td>
432 </tr>
433 <tr class="odd">
434 <td>cat</td>
435 <td>0</td>
436 <td>1</td>
437 </tr>
438 </tbody>
439 </table>
440 <p>With an example like this, it is fairly easy to make the dummy
441 columns yourself. <code>dummy_cols()</code> automates the process, and
442 is useful when you have many columns to general dummy variables from or
443 with many categories within the column.</p>
444 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a>fastDummies_example <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(<span class="at">numbers =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>,</span>
445 <span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a> <span class="at">gender =</span> <span class="fu">c</span>(<span class="st">&quot;male&quot;</span>, <span class="st">&quot;male&quot;</span>, <span class="st">&quot;female&quot;</span>),</span>
446 <span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a> <span class="at">animals =</span> <span class="fu">c</span>(<span class="st">&quot;dog&quot;</span>, <span class="st">&quot;dog&quot;</span>, <span class="st">&quot;cat&quot;</span>),</span>
447 <span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a> <span class="at">dates =</span> <span class="fu">as.Date</span>(<span class="fu">c</span>(<span class="st">&quot;2012-01-01&quot;</span>, <span class="st">&quot;2011-12-31&quot;</span>,</span>
448 <span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a> <span class="st">&quot;2012-01-01&quot;</span>)),</span>
449 <span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a> <span class="at">stringsAsFactors =</span> <span class="cn">FALSE</span>)</span>
450 <span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(fastDummies_example)</span></code></pre></div>
451 <table>
452 <thead>
453 <tr class="header">
454 <th align="right">numbers</th>
455 <th align="left">gender</th>
456 <th align="left">animals</th>
457 <th align="left">dates</th>
458 </tr>
459 </thead>
460 <tbody>
461 <tr class="odd">
462 <td align="right">1</td>
463 <td align="left">male</td>
464 <td align="left">dog</td>
465 <td align="left">2012-01-01</td>
466 </tr>
467 <tr class="even">
468 <td align="right">2</td>
469 <td align="left">male</td>
470 <td align="left">dog</td>
471 <td align="left">2011-12-31</td>
472 </tr>
473 <tr class="odd">
474 <td align="right">3</td>
475 <td align="left">female</td>
476 <td align="left">cat</td>
477 <td align="left">2012-01-01</td>
478 </tr>
479 </tbody>
480 </table>
481 <p>The object <strong>fastDummies_example</strong> has two character
482 type columns, one integer column, and a Date column. By default,
483 <code>dummy_cols()</code> will make dummy variables from factor or
484 character columns only. This is because in most cases those are the only
485 types of data you want dummy variables from. If those are the only
486 columns you want, then the function takes your data set as the first
487 parameter and returns a data.frame with the newly created variables
488 appended to the end of the original data.</p>
489 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>results <span class="ot">&lt;-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_cols</span>(fastDummies_example)</span>
490 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(results)</span></code></pre></div>
491 <table style="width:100%;">
492 <colgroup>
493 <col width="9%" />
494 <col width="8%" />
495 <col width="9%" />
496 <col width="13%" />
497 <col width="16%" />
498 <col width="14%" />
499 <col width="14%" />
500 <col width="14%" />
501 </colgroup>
502 <thead>
503 <tr class="header">
504 <th align="right">numbers</th>
505 <th align="left">gender</th>
506 <th align="left">animals</th>
507 <th align="left">dates</th>
508 <th align="right">gender_female</th>
509 <th align="right">gender_male</th>
510 <th align="right">animals_cat</th>
511 <th align="right">animals_dog</th>
512 </tr>
513 </thead>
514 <tbody>
515 <tr class="odd">
516 <td align="right">1</td>
517 <td align="left">male</td>
518 <td align="left">dog</td>
519 <td align="left">2012-01-01</td>
520 <td align="right">0</td>
521 <td align="right">1</td>
522 <td align="right">0</td>
523 <td align="right">1</td>
524 </tr>
525 <tr class="even">
526 <td align="right">2</td>
527 <td align="left">male</td>
528 <td align="left">dog</td>
529 <td align="left">2011-12-31</td>
530 <td align="right">0</td>
531 <td align="right">1</td>
532 <td align="right">0</td>
533 <td align="right">1</td>
534 </tr>
535 <tr class="odd">
536 <td align="right">3</td>
537 <td align="left">female</td>
538 <td align="left">cat</td>
539 <td align="left">2012-01-01</td>
540 <td align="right">1</td>
541 <td align="right">0</td>
542 <td align="right">1</td>
543 <td align="right">0</td>
544 </tr>
545 </tbody>
546 </table>
547 <p>In some situations, you would want columns with types other than
548 factor and character to generate dummy variables. For example, a column
549 of years would be numeric but could be well-suited for making into dummy
550 variables depending on your analysis. Use the <em>select_columns</em>
551 parameter to select specific columns to make dummy variables from.</p>
552 <div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>results <span class="ot">&lt;-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_cols</span>(fastDummies_example, <span class="at">select_columns =</span> <span class="st">&quot;numbers&quot;</span>)</span>
553 <span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(results)</span></code></pre></div>
554 <table>
555 <thead>
556 <tr class="header">
557 <th align="right">numbers</th>
558 <th align="left">gender</th>
559 <th align="left">animals</th>
560 <th align="left">dates</th>
561 <th align="right">numbers_1</th>
562 <th align="right">numbers_2</th>
563 <th align="right">numbers_3</th>
564 </tr>
565 </thead>
566 <tbody>
567 <tr class="odd">
568 <td align="right">1</td>
569 <td align="left">male</td>
570 <td align="left">dog</td>
571 <td align="left">2012-01-01</td>
572 <td align="right">1</td>
573 <td align="right">0</td>
574 <td align="right">0</td>
575 </tr>
576 <tr class="even">
577 <td align="right">2</td>
578 <td align="left">male</td>
579 <td align="left">dog</td>
580 <td align="left">2011-12-31</td>
581 <td align="right">0</td>
582 <td align="right">1</td>
583 <td align="right">0</td>
584 </tr>
585 <tr class="odd">
586 <td align="right">3</td>
587 <td align="left">female</td>
588 <td align="left">cat</td>
589 <td align="left">2012-01-01</td>
590 <td align="right">0</td>
591 <td align="right">0</td>
592 <td align="right">1</td>
593 </tr>
594 </tbody>
595 </table>
596 <p>The final option for <code>dummy_cols()</code> is
597 <em>remove_first_dummy</em> which by default is FALSE. If TRUE, it
598 removes the first dummy variable created from each column. This is done
599 to avoid multicollinearity in a multiple regression model caused by
600 included all dummy variables. The “first” dummy variable is the one at
601 the top of the rows (i.e. the first value that is not NA).</p>
602 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>results <span class="ot">&lt;-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_cols</span>(fastDummies_example, <span class="at">remove_first_dummy =</span> <span class="cn">TRUE</span>)</span>
603 <span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(results)</span></code></pre></div>
604 <table>
605 <thead>
606 <tr class="header">
607 <th align="right">numbers</th>
608 <th align="left">gender</th>
609 <th align="left">animals</th>
610 <th align="left">dates</th>
611 <th align="right">gender_male</th>
612 <th align="right">animals_dog</th>
613 </tr>
614 </thead>
615 <tbody>
616 <tr class="odd">
617 <td align="right">1</td>
618 <td align="left">male</td>
619 <td align="left">dog</td>
620 <td align="left">2012-01-01</td>
621 <td align="right">1</td>
622 <td align="right">1</td>
623 </tr>
624 <tr class="even">
625 <td align="right">2</td>
626 <td align="left">male</td>
627 <td align="left">dog</td>
628 <td align="left">2011-12-31</td>
629 <td align="right">1</td>
630 <td align="right">1</td>
631 </tr>
632 <tr class="odd">
633 <td align="right">3</td>
634 <td align="left">female</td>
635 <td align="left">cat</td>
636 <td align="left">2012-01-01</td>
637 <td align="right">0</td>
638 <td align="right">0</td>
639 </tr>
640 </tbody>
641 </table>
642
643
644
645 <!-- code folding -->
646
647
648 <!-- dynamically load mathjax for compatibility with self-contained -->
649 <script>
650 (function () {
651 var script = document.createElement("script");
652 script.type = "text/javascript";
653 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
654 document.getElementsByTagName("head")[0].appendChild(script);
655 })();
656 </script>
657
658 </body>
659 </html>