| 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 rows with dummy_rows()</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 rows with dummy_rows()</h1> |
| 343 |
<h4 class="author">Jacob Kaplan</h4> |
| 344 |
<h4 class="date">2026-04-22</h4> |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
<p>When dealing with data, there are often missing rows. While truly |
| 349 |
handling missing data is far beyond the scope of this package, the |
| 350 |
function <code>dummy_rows()</code> lets you add those missing rows back |
| 351 |
into the data.</p> |
| 352 |
<p>The function takes all character, factor, and Date columns, finds all |
| 353 |
possible combinations of their values, and adds the rows that are not in |
| 354 |
the original data set. Any columns not used in creating the combinations |
| 355 |
(e.g. numeric) are given a value of NA (unless otherwise specified with |
| 356 |
<em>dummy_value</em>).</p> |
| 357 |
<p>Lets start with a simple example.</p> |
| 358 |
<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"><-</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> |
| 359 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a> <span class="at">gender =</span> <span class="fu">c</span>(<span class="st">"male"</span>, <span class="st">"male"</span>, <span class="st">"female"</span>),</span> |
| 360 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a> <span class="at">animals =</span> <span class="fu">c</span>(<span class="st">"dog"</span>, <span class="st">"dog"</span>, <span class="st">"cat"</span>),</span> |
| 361 |
<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">"2012-01-01"</span>, <span class="st">"2011-12-31"</span>,</span> |
| 362 |
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a> <span class="st">"2012-01-01"</span>)),</span> |
| 363 |
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a> <span class="at">stringsAsFactors =</span> <span class="cn">FALSE</span>)</span> |
| 364 |
<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> |
| 365 |
<table> |
| 366 |
<thead> |
| 367 |
<tr class="header"> |
| 368 |
<th align="right">numbers</th> |
| 369 |
<th align="left">gender</th> |
| 370 |
<th align="left">animals</th> |
| 371 |
<th align="left">dates</th> |
| 372 |
</tr> |
| 373 |
</thead> |
| 374 |
<tbody> |
| 375 |
<tr class="odd"> |
| 376 |
<td align="right">1</td> |
| 377 |
<td align="left">male</td> |
| 378 |
<td align="left">dog</td> |
| 379 |
<td align="left">2012-01-01</td> |
| 380 |
</tr> |
| 381 |
<tr class="even"> |
| 382 |
<td align="right">2</td> |
| 383 |
<td align="left">male</td> |
| 384 |
<td align="left">dog</td> |
| 385 |
<td align="left">2011-12-31</td> |
| 386 |
</tr> |
| 387 |
<tr class="odd"> |
| 388 |
<td align="right">3</td> |
| 389 |
<td align="left">female</td> |
| 390 |
<td align="left">cat</td> |
| 391 |
<td align="left">2012-01-01</td> |
| 392 |
</tr> |
| 393 |
</tbody> |
| 394 |
</table> |
| 395 |
<p>This data set has four columns: two character, one Date, and one |
| 396 |
numeric. The function by default will use the character and Date columns |
| 397 |
in creating the combinations. First, a small amount of math to explain |
| 398 |
the combinations. Each column has two distinct values - gender: male |
| 399 |
& female; animals: dog & cat; dates: 2011-12-31 & |
| 400 |
2011-12-31. To find the number of possible combinations, multiple the |
| 401 |
number of unique values in each column together. 2 * 2 * 2 = 8.</p> |
| 402 |
<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"><-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_rows</span>(fastDummies_example)</span> |
| 403 |
<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> |
| 404 |
<table> |
| 405 |
<thead> |
| 406 |
<tr class="header"> |
| 407 |
<th align="right">numbers</th> |
| 408 |
<th align="left">gender</th> |
| 409 |
<th align="left">animals</th> |
| 410 |
<th align="left">dates</th> |
| 411 |
</tr> |
| 412 |
</thead> |
| 413 |
<tbody> |
| 414 |
<tr class="odd"> |
| 415 |
<td align="right">1</td> |
| 416 |
<td align="left">male</td> |
| 417 |
<td align="left">dog</td> |
| 418 |
<td align="left">2012-01-01</td> |
| 419 |
</tr> |
| 420 |
<tr class="even"> |
| 421 |
<td align="right">2</td> |
| 422 |
<td align="left">male</td> |
| 423 |
<td align="left">dog</td> |
| 424 |
<td align="left">2011-12-31</td> |
| 425 |
</tr> |
| 426 |
<tr class="odd"> |
| 427 |
<td align="right">3</td> |
| 428 |
<td align="left">female</td> |
| 429 |
<td align="left">cat</td> |
| 430 |
<td align="left">2012-01-01</td> |
| 431 |
</tr> |
| 432 |
<tr class="even"> |
| 433 |
<td align="right">NA</td> |
| 434 |
<td align="left">female</td> |
| 435 |
<td align="left">cat</td> |
| 436 |
<td align="left">2011-12-31</td> |
| 437 |
</tr> |
| 438 |
<tr class="odd"> |
| 439 |
<td align="right">NA</td> |
| 440 |
<td align="left">male</td> |
| 441 |
<td align="left">cat</td> |
| 442 |
<td align="left">2011-12-31</td> |
| 443 |
</tr> |
| 444 |
<tr class="even"> |
| 445 |
<td align="right">NA</td> |
| 446 |
<td align="left">female</td> |
| 447 |
<td align="left">dog</td> |
| 448 |
<td align="left">2011-12-31</td> |
| 449 |
</tr> |
| 450 |
<tr class="odd"> |
| 451 |
<td align="right">NA</td> |
| 452 |
<td align="left">male</td> |
| 453 |
<td align="left">cat</td> |
| 454 |
<td align="left">2012-01-01</td> |
| 455 |
</tr> |
| 456 |
<tr class="even"> |
| 457 |
<td align="right">NA</td> |
| 458 |
<td align="left">female</td> |
| 459 |
<td align="left">dog</td> |
| 460 |
<td align="left">2012-01-01</td> |
| 461 |
</tr> |
| 462 |
</tbody> |
| 463 |
</table> |
| 464 |
<p>When we run the function we can see that there are indeed 8 rows |
| 465 |
possible, and that the 5 rows missing from the original data have been |
| 466 |
added.</p> |
| 467 |
<p>To explicitly see which rows are new, set the |
| 468 |
<em>dummy_indicator</em> parameter to TRUE. This provides a column |
| 469 |
called dummy_indicator with a value of 0 if the row is in the original |
| 470 |
data and 1 if it was added.</p> |
| 471 |
<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"><-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_rows</span>(fastDummies_example, <span class="at">dummy_indicator =</span> <span class="cn">TRUE</span>)</span> |
| 472 |
<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> |
| 473 |
<table> |
| 474 |
<thead> |
| 475 |
<tr class="header"> |
| 476 |
<th align="right">numbers</th> |
| 477 |
<th align="left">gender</th> |
| 478 |
<th align="left">animals</th> |
| 479 |
<th align="left">dates</th> |
| 480 |
<th align="right">dummy_indicator</th> |
| 481 |
</tr> |
| 482 |
</thead> |
| 483 |
<tbody> |
| 484 |
<tr class="odd"> |
| 485 |
<td align="right">1</td> |
| 486 |
<td align="left">male</td> |
| 487 |
<td align="left">dog</td> |
| 488 |
<td align="left">2012-01-01</td> |
| 489 |
<td align="right">0</td> |
| 490 |
</tr> |
| 491 |
<tr class="even"> |
| 492 |
<td align="right">2</td> |
| 493 |
<td align="left">male</td> |
| 494 |
<td align="left">dog</td> |
| 495 |
<td align="left">2011-12-31</td> |
| 496 |
<td align="right">0</td> |
| 497 |
</tr> |
| 498 |
<tr class="odd"> |
| 499 |
<td align="right">3</td> |
| 500 |
<td align="left">female</td> |
| 501 |
<td align="left">cat</td> |
| 502 |
<td align="left">2012-01-01</td> |
| 503 |
<td align="right">0</td> |
| 504 |
</tr> |
| 505 |
<tr class="even"> |
| 506 |
<td align="right">NA</td> |
| 507 |
<td align="left">female</td> |
| 508 |
<td align="left">cat</td> |
| 509 |
<td align="left">2011-12-31</td> |
| 510 |
<td align="right">1</td> |
| 511 |
</tr> |
| 512 |
<tr class="odd"> |
| 513 |
<td align="right">NA</td> |
| 514 |
<td align="left">male</td> |
| 515 |
<td align="left">cat</td> |
| 516 |
<td align="left">2011-12-31</td> |
| 517 |
<td align="right">1</td> |
| 518 |
</tr> |
| 519 |
<tr class="even"> |
| 520 |
<td align="right">NA</td> |
| 521 |
<td align="left">female</td> |
| 522 |
<td align="left">dog</td> |
| 523 |
<td align="left">2011-12-31</td> |
| 524 |
<td align="right">1</td> |
| 525 |
</tr> |
| 526 |
<tr class="odd"> |
| 527 |
<td align="right">NA</td> |
| 528 |
<td align="left">male</td> |
| 529 |
<td align="left">cat</td> |
| 530 |
<td align="left">2012-01-01</td> |
| 531 |
<td align="right">1</td> |
| 532 |
</tr> |
| 533 |
<tr class="even"> |
| 534 |
<td align="right">NA</td> |
| 535 |
<td align="left">female</td> |
| 536 |
<td align="left">dog</td> |
| 537 |
<td align="left">2012-01-01</td> |
| 538 |
<td align="right">1</td> |
| 539 |
</tr> |
| 540 |
</tbody> |
| 541 |
</table> |
| 542 |
<p>By default, columns not used for making the combinations are given a |
| 543 |
value of NA in the new rows. You can choose the value given with the |
| 544 |
parameter <em>dummy_value</em>. It takes an input, a string or single |
| 545 |
number.</p> |
| 546 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>results1 <span class="ot"><-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_rows</span>(fastDummies_example, <span class="at">dummy_value =</span> <span class="dv">0</span>)</span> |
| 547 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>results2 <span class="ot"><-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_rows</span>(fastDummies_example, <span class="at">dummy_value =</span> <span class="st">"new value"</span>)</span> |
| 548 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(results1)</span></code></pre></div> |
| 549 |
<table> |
| 550 |
<thead> |
| 551 |
<tr class="header"> |
| 552 |
<th align="right">numbers</th> |
| 553 |
<th align="left">gender</th> |
| 554 |
<th align="left">animals</th> |
| 555 |
<th align="left">dates</th> |
| 556 |
</tr> |
| 557 |
</thead> |
| 558 |
<tbody> |
| 559 |
<tr class="odd"> |
| 560 |
<td align="right">1</td> |
| 561 |
<td align="left">male</td> |
| 562 |
<td align="left">dog</td> |
| 563 |
<td align="left">2012-01-01</td> |
| 564 |
</tr> |
| 565 |
<tr class="even"> |
| 566 |
<td align="right">2</td> |
| 567 |
<td align="left">male</td> |
| 568 |
<td align="left">dog</td> |
| 569 |
<td align="left">2011-12-31</td> |
| 570 |
</tr> |
| 571 |
<tr class="odd"> |
| 572 |
<td align="right">3</td> |
| 573 |
<td align="left">female</td> |
| 574 |
<td align="left">cat</td> |
| 575 |
<td align="left">2012-01-01</td> |
| 576 |
</tr> |
| 577 |
<tr class="even"> |
| 578 |
<td align="right">0</td> |
| 579 |
<td align="left">female</td> |
| 580 |
<td align="left">cat</td> |
| 581 |
<td align="left">2011-12-31</td> |
| 582 |
</tr> |
| 583 |
<tr class="odd"> |
| 584 |
<td align="right">0</td> |
| 585 |
<td align="left">male</td> |
| 586 |
<td align="left">cat</td> |
| 587 |
<td align="left">2011-12-31</td> |
| 588 |
</tr> |
| 589 |
<tr class="even"> |
| 590 |
<td align="right">0</td> |
| 591 |
<td align="left">female</td> |
| 592 |
<td align="left">dog</td> |
| 593 |
<td align="left">2011-12-31</td> |
| 594 |
</tr> |
| 595 |
<tr class="odd"> |
| 596 |
<td align="right">0</td> |
| 597 |
<td align="left">male</td> |
| 598 |
<td align="left">cat</td> |
| 599 |
<td align="left">2012-01-01</td> |
| 600 |
</tr> |
| 601 |
<tr class="even"> |
| 602 |
<td align="right">0</td> |
| 603 |
<td align="left">female</td> |
| 604 |
<td align="left">dog</td> |
| 605 |
<td align="left">2012-01-01</td> |
| 606 |
</tr> |
| 607 |
</tbody> |
| 608 |
</table> |
| 609 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(results2)</span></code></pre></div> |
| 610 |
<table> |
| 611 |
<thead> |
| 612 |
<tr class="header"> |
| 613 |
<th align="left">numbers</th> |
| 614 |
<th align="left">gender</th> |
| 615 |
<th align="left">animals</th> |
| 616 |
<th align="left">dates</th> |
| 617 |
</tr> |
| 618 |
</thead> |
| 619 |
<tbody> |
| 620 |
<tr class="odd"> |
| 621 |
<td align="left">1</td> |
| 622 |
<td align="left">male</td> |
| 623 |
<td align="left">dog</td> |
| 624 |
<td align="left">2012-01-01</td> |
| 625 |
</tr> |
| 626 |
<tr class="even"> |
| 627 |
<td align="left">2</td> |
| 628 |
<td align="left">male</td> |
| 629 |
<td align="left">dog</td> |
| 630 |
<td align="left">2011-12-31</td> |
| 631 |
</tr> |
| 632 |
<tr class="odd"> |
| 633 |
<td align="left">3</td> |
| 634 |
<td align="left">female</td> |
| 635 |
<td align="left">cat</td> |
| 636 |
<td align="left">2012-01-01</td> |
| 637 |
</tr> |
| 638 |
<tr class="even"> |
| 639 |
<td align="left">new value</td> |
| 640 |
<td align="left">female</td> |
| 641 |
<td align="left">cat</td> |
| 642 |
<td align="left">2011-12-31</td> |
| 643 |
</tr> |
| 644 |
<tr class="odd"> |
| 645 |
<td align="left">new value</td> |
| 646 |
<td align="left">male</td> |
| 647 |
<td align="left">cat</td> |
| 648 |
<td align="left">2011-12-31</td> |
| 649 |
</tr> |
| 650 |
<tr class="even"> |
| 651 |
<td align="left">new value</td> |
| 652 |
<td align="left">female</td> |
| 653 |
<td align="left">dog</td> |
| 654 |
<td align="left">2011-12-31</td> |
| 655 |
</tr> |
| 656 |
<tr class="odd"> |
| 657 |
<td align="left">new value</td> |
| 658 |
<td align="left">male</td> |
| 659 |
<td align="left">cat</td> |
| 660 |
<td align="left">2012-01-01</td> |
| 661 |
</tr> |
| 662 |
<tr class="even"> |
| 663 |
<td align="left">new value</td> |
| 664 |
<td align="left">female</td> |
| 665 |
<td align="left">dog</td> |
| 666 |
<td align="left">2012-01-01</td> |
| 667 |
</tr> |
| 668 |
</tbody> |
| 669 |
</table> |
| 670 |
<p>The parameter <em>select_columns</em> lets you choose which columns |
| 671 |
to use when making the combinations. It accepts a string or vector of |
| 672 |
column names. This can come in handy when you want to include a numeric |
| 673 |
column, such as years, when making the combinations. A new data set will |
| 674 |
help demonstrate this. This data set shows (imaginary) crime in New York |
| 675 |
City and San Francisco during 1990 and 2000. The problem is that there |
| 676 |
is no row for New York City for 2000. We want to add that row.</p> |
| 677 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a>crime <span class="ot"><-</span> <span class="fu">data.frame</span>(<span class="at">city =</span> <span class="fu">c</span>(<span class="st">"SF"</span>, <span class="st">"SF"</span>, <span class="st">"NYC"</span>),</span> |
| 678 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">c</span>(<span class="dv">1990</span>, <span class="dv">2000</span>, <span class="dv">1990</span>),</span> |
| 679 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a> <span class="at">crime =</span> <span class="dv">1</span><span class="sc">:</span><span class="dv">3</span>)</span> |
| 680 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(crime)</span></code></pre></div> |
| 681 |
<table> |
| 682 |
<thead> |
| 683 |
<tr class="header"> |
| 684 |
<th align="left">city</th> |
| 685 |
<th align="right">year</th> |
| 686 |
<th align="right">crime</th> |
| 687 |
</tr> |
| 688 |
</thead> |
| 689 |
<tbody> |
| 690 |
<tr class="odd"> |
| 691 |
<td align="left">SF</td> |
| 692 |
<td align="right">1990</td> |
| 693 |
<td align="right">1</td> |
| 694 |
</tr> |
| 695 |
<tr class="even"> |
| 696 |
<td align="left">SF</td> |
| 697 |
<td align="right">2000</td> |
| 698 |
<td align="right">2</td> |
| 699 |
</tr> |
| 700 |
<tr class="odd"> |
| 701 |
<td align="left">NYC</td> |
| 702 |
<td align="right">1990</td> |
| 703 |
<td align="right">3</td> |
| 704 |
</tr> |
| 705 |
</tbody> |
| 706 |
</table> |
| 707 |
<p>Using the default parameters for <code>dummy_rows()</code> doesn’t |
| 708 |
give us what we want since it only selects the city column. We need to |
| 709 |
select both city and year to get all the combinations we want.</p> |
| 710 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>results <span class="ot"><-</span> fastDummies<span class="sc">::</span><span class="fu">dummy_rows</span>(crime, <span class="at">select_columns =</span> <span class="fu">c</span>(<span class="st">"city"</span>, <span class="st">"year"</span>))</span> |
| 711 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a>knitr<span class="sc">::</span><span class="fu">kable</span>(results)</span></code></pre></div> |
| 712 |
<table> |
| 713 |
<thead> |
| 714 |
<tr class="header"> |
| 715 |
<th align="left">city</th> |
| 716 |
<th align="right">year</th> |
| 717 |
<th align="right">crime</th> |
| 718 |
</tr> |
| 719 |
</thead> |
| 720 |
<tbody> |
| 721 |
<tr class="odd"> |
| 722 |
<td align="left">SF</td> |
| 723 |
<td align="right">1990</td> |
| 724 |
<td align="right">1</td> |
| 725 |
</tr> |
| 726 |
<tr class="even"> |
| 727 |
<td align="left">SF</td> |
| 728 |
<td align="right">2000</td> |
| 729 |
<td align="right">2</td> |
| 730 |
</tr> |
| 731 |
<tr class="odd"> |
| 732 |
<td align="left">NYC</td> |
| 733 |
<td align="right">1990</td> |
| 734 |
<td align="right">3</td> |
| 735 |
</tr> |
| 736 |
<tr class="even"> |
| 737 |
<td align="left">NYC</td> |
| 738 |
<td align="right">2000</td> |
| 739 |
<td align="right">NA</td> |
| 740 |
</tr> |
| 741 |
</tbody> |
| 742 |
</table> |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
<!-- code folding --> |
| 747 |
|
| 748 |
|
| 749 |
<!-- dynamically load mathjax for compatibility with self-contained --> |
| 750 |
<script> |
| 751 |
(function () { |
| 752 |
var script = document.createElement("script"); |
| 753 |
script.type = "text/javascript"; |
| 754 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 755 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 756 |
})(); |
| 757 |
</script> |
| 758 |
|
| 759 |
</body> |
| 760 |
</html> |