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
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="Daniel Lüdecke" />
<meta name="date" content="2024-05-13" />
<title>The Design Philosophy of Functions in sjmisc</title>
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
// be compatible with the behavior of Pandoc < 2.8).
document.addEventListener('DOMContentLoaded', function(e) {
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
var i, h, a;
for (i = 0; i < hs.length; i++) {
h = hs[i];
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
a = h.attributes;
while (a.length > 0) h.removeAttribute(a[0].name);
}
});
</script>
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">
code {
white-space: pre;
}
.sourceCode {
overflow: visible;
}
</style>
<style type="text/css" data-origin="pandoc">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { 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 { display: inline-block; text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; }
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.at { color: #7d9029; }
code span.bn { color: #40a070; }
code span.bu { color: #008000; }
code span.cf { color: #007020; font-weight: bold; }
code span.ch { color: #4070a0; }
code span.cn { color: #880000; }
code span.co { color: #60a0b0; font-style: italic; }
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.do { color: #ba2121; font-style: italic; }
code span.dt { color: #902000; }
code span.dv { color: #40a070; }
code span.er { color: #ff0000; font-weight: bold; }
code span.ex { }
code span.fl { color: #40a070; }
code span.fu { color: #06287e; }
code span.im { color: #008000; font-weight: bold; }
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
code span.kw { color: #007020; font-weight: bold; }
code span.op { color: #666666; }
code span.ot { color: #007020; }
code span.pp { color: #bc7a00; }
code span.sc { color: #4070a0; }
code span.ss { color: #bb6688; }
code span.st { color: #4070a0; }
code span.va { color: #19177c; }
code span.vs { color: #4070a0; }
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
var j = 0;
while (j < rules.length) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
j++;
continue;
}
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') {
j++;
continue;
}
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">body {
background-color: #fff;
margin: 1em auto;
max-width: 700px;
overflow: visible;
padding-left: 2em;
padding-right: 2em;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 14px;
line-height: 1.35;
}
#TOC {
clear: both;
margin: 0 0 10px 10px;
padding: 4px;
width: 400px;
border: 1px solid #CCCCCC;
border-radius: 5px;
background-color: #f6f6f6;
font-size: 13px;
line-height: 1.3;
}
#TOC .toctitle {
font-weight: bold;
font-size: 15px;
margin-left: 5px;
}
#TOC ul {
padding-left: 40px;
margin-left: -1.5em;
margin-top: 5px;
margin-bottom: 5px;
}
#TOC ul ul {
margin-left: -2em;
}
#TOC li {
line-height: 16px;
}
table {
margin: 1em auto;
border-width: 1px;
border-color: #DDDDDD;
border-style: outset;
border-collapse: collapse;
}
table th {
border-width: 2px;
padding: 5px;
border-style: inset;
}
table td {
border-width: 1px;
border-style: inset;
line-height: 18px;
padding: 5px 5px;
}
table, table th, table td {
border-left-style: none;
border-right-style: none;
}
table thead, table tr.even {
background-color: #f7f7f7;
}
p {
margin: 0.5em 0;
}
blockquote {
background-color: #f6f6f6;
padding: 0.25em 0.75em;
}
hr {
border-style: solid;
border: none;
border-top: 1px solid #777;
margin: 28px 0;
}
dl {
margin-left: 0;
}
dl dd {
margin-bottom: 13px;
margin-left: 13px;
}
dl dt {
font-weight: bold;
}
ul {
margin-top: 0;
}
ul li {
list-style: circle outside;
}
ul ul {
margin-bottom: 0;
}
pre, code {
background-color: #f7f7f7;
border-radius: 3px;
color: #333;
white-space: pre-wrap;
}
pre {
border-radius: 3px;
margin: 5px 0px 10px 0px;
padding: 10px;
}
pre:not([class]) {
background-color: #f7f7f7;
}
code {
font-family: Consolas, Monaco, 'Courier New', monospace;
font-size: 85%;
}
p > code, li > code {
padding: 2px 0px;
}
div.figure {
text-align: center;
}
img {
background-color: #FFFFFF;
padding: 2px;
border: 1px solid #DDDDDD;
border-radius: 3px;
border: 1px solid #CCCCCC;
margin: 0 5px;
}
h1 {
margin-top: 0;
font-size: 35px;
line-height: 40px;
}
h2 {
border-bottom: 4px solid #f7f7f7;
padding-top: 10px;
padding-bottom: 2px;
font-size: 145%;
}
h3 {
border-bottom: 2px solid #f7f7f7;
padding-top: 10px;
font-size: 120%;
}
h4 {
border-bottom: 1px solid #f7f7f7;
margin-left: 8px;
font-size: 105%;
}
h5, h6 {
border-bottom: 1px solid #ccc;
font-size: 105%;
}
a {
color: #0033dd;
text-decoration: none;
}
a:hover {
color: #6666ff; }
a:visited {
color: #800080; }
a:visited:hover {
color: #BB00BB; }
a[href^="http:"] {
text-decoration: underline; }
a[href^="https:"] {
text-decoration: underline; }
code > span.kw { color: #555; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #d14; }
code > span.fl { color: #d14; }
code > span.ch { color: #d14; }
code > span.st { color: #d14; }
code > span.co { color: #888888; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #900; font-weight: bold; }
code > span.er { color: #a61717; background-color: #e3d2d2; }
</style>
</head>
<body>
<h1 class="title toc-ignore">The Design Philosophy of Functions in
sjmisc</h1>
<h4 class="author">Daniel Lüdecke</h4>
<h4 class="date">2024-05-13</h4>
<p>Basically, this package complements the <em>dplyr</em> package in
that <em>sjmisc</em> takes over data transformation tasks on variables,
like recoding, dichotomizing or grouping variables, setting and
replacing missing values, etc. The data transformation functions also
support labelled data.</p>
<div id="the-design-of-data-transformation-functions" class="section level1">
<h1>The design of data transformation functions</h1>
<p>The design of data transformation functions in this package follows,
where appropriate, the <em>tidyverse-approach</em>, with the first
argument of a function always being the data (either a data frame or
vector), followed by variable names that should be processed by the
function. If no variables are specified as argument, the function
applies to the complete data that was indicated as first function
argument.</p>
<div id="the-data-argument" class="section level2">
<h2>The data-argument</h2>
<p>A major difference to dplyr-functions like <code>select()</code> or
<code>filter()</code> is that the data-argument (the first argument of
each function), may either be a <em>data frame</em> or a
<em>vector</em>. The returned object for each function <em>equals the
type of the data-argument</em>:</p>
<ul>
<li>If the data-argument is a vector, the function returns a
vector.</li>
<li>If the data-argument is a data frame, the function returns a data
frame.</li>
</ul>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(sjmisc)</span>
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">data</span>(efc)</span>
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="co"># returns a vector</span></span>
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">rec</span>(efc<span class="sc">$</span>e42dep, <span class="at">rec =</span> <span class="st">"1,2=1; 3,4=2"</span>)</span>
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a><span class="fu">str</span>(x)</span>
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a><span class="co">#> num [1:908] 2 2 2 2 2 2 2 2 2 2 ...</span></span>
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a><span class="co">#> - attr(*, "label")= chr "elder's dependency"</span></span>
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a></span>
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a><span class="co"># returns a data frame</span></span>
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a><span class="fu">rec</span>(efc, e42dep, <span class="at">rec =</span> <span class="st">"1,2=1; 3,4=2"</span>, <span class="at">append =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">head</span>()</span>
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a><span class="co">#> e42dep_r</span></span>
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a><span class="co">#> 1 2</span></span>
<span id="cb1-14"><a href="#cb1-14" tabindex="-1"></a><span class="co">#> 2 2</span></span>
<span id="cb1-15"><a href="#cb1-15" tabindex="-1"></a><span class="co">#> 3 2</span></span>
<span id="cb1-16"><a href="#cb1-16" tabindex="-1"></a><span class="co">#> 4 2</span></span>
<span id="cb1-17"><a href="#cb1-17" tabindex="-1"></a><span class="co">#> 5 2</span></span>
<span id="cb1-18"><a href="#cb1-18" tabindex="-1"></a><span class="co">#> 6 2</span></span></code></pre></div>
<p>This design-choice is mainly due to compatibility- and
convenience-reasons. It does not affect the usual “tidyverse-workflow”
or when using pipe-chains.</p>
</div>
<div id="the--ellipses-argument" class="section level2">
<h2>The …-ellipses-argument</h2>
<p>The selection of variables specified in the
<code>...</code>-ellipses-argument is powered by dplyr’s
<code>select()</code> and tidyselect’s <code>select_helpers()</code>.
This means, you can use existing functions like <code>:</code> to select
a range of variables, or also use tidyselect’s
<code>select_helpers</code>, like <code>contains()</code> or
<code>one_of()</code>.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a><span class="co"># select all variables with "cop" in their names, and also</span></span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="co"># the range from c161sex to c175empl</span></span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="fu">rec</span>(</span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> efc, <span class="fu">contains</span>(<span class="st">"cop"</span>), c161sex<span class="sc">:</span>c175empl, </span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> <span class="at">rec =</span> <span class="st">"0,1=0; else=1"</span>, </span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a> <span class="at">append =</span> <span class="cn">FALSE</span></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a>) <span class="sc">%>%</span> <span class="fu">head</span>()</span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#> c82cop1_r c83cop2_r c84cop3_r c85cop4_r c86cop5_r c87cop6_r c88cop7_r</span></span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#> 1 1 1 1 1 0 0 1</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#> 2 1 1 1 1 1 0 1</span></span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co">#> 3 1 1 0 1 0 0 0</span></span>
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co">#> 4 1 0 1 0 0 0 0</span></span>
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a><span class="co">#> 5 1 1 0 1 1 1 0</span></span>
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a><span class="co">#> 6 1 1 1 1 1 1 1</span></span>
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a><span class="co">#> c89cop8_r c90cop9_r c161sex_r c172code_r c175empl_r</span></span>
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a><span class="co">#> 1 1 1 1 1 0</span></span>
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a><span class="co">#> 2 1 1 1 1 0</span></span>
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a><span class="co">#> 3 1 1 0 0 0</span></span>
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a><span class="co">#> 4 1 1 0 1 0</span></span>
<span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a><span class="co">#> 5 1 1 1 1 0</span></span>
<span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a><span class="co">#> 6 0 0 0 1 0</span></span>
<span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a></span>
<span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a><span class="co"># center all variables with "age" in name, variable c12hour</span></span>
<span id="cb2-24"><a href="#cb2-24" tabindex="-1"></a><span class="co"># and all variables from column 19 to 21</span></span>
<span id="cb2-25"><a href="#cb2-25" tabindex="-1"></a><span class="fu">center</span>(efc, c12hour, <span class="fu">contains</span>(<span class="st">"age"</span>), <span class="dv">19</span><span class="sc">:</span><span class="dv">21</span>, <span class="at">append =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">head</span>()</span>
<span id="cb2-26"><a href="#cb2-26" tabindex="-1"></a><span class="co">#> c12hour_c e17age_c c160age_c barthtot_c neg_c_7_c pos_v_4_c</span></span>
<span id="cb2-27"><a href="#cb2-27" tabindex="-1"></a><span class="co">#> 1 -26.39911 3.878788 2.5371809 10.453001 0.1502242 -0.476731</span></span>
<span id="cb2-28"><a href="#cb2-28" tabindex="-1"></a><span class="co">#> 2 105.60089 8.878788 0.5371809 10.453001 8.1502242 -1.476731</span></span>
<span id="cb2-29"><a href="#cb2-29" tabindex="-1"></a><span class="co">#> 3 27.60089 2.878788 26.5371809 -29.546999 -0.8497758 0.523269</span></span>
<span id="cb2-30"><a href="#cb2-30" tabindex="-1"></a><span class="co">#> 4 125.60089 -12.121212 15.5371809 -64.546999 -1.8497758 2.523269</span></span>
<span id="cb2-31"><a href="#cb2-31" tabindex="-1"></a><span class="co">#> 5 125.60089 4.878788 -6.4628191 -39.546999 0.1502242 2.523269</span></span>
<span id="cb2-32"><a href="#cb2-32" tabindex="-1"></a><span class="co">#> 6 -26.39911 5.878788 2.5371809 -4.546999 7.1502242 -3.476731</span></span></code></pre></div>
</div>
<div id="the-function-types" class="section level2">
<h2>The function-types</h2>
<p>There are two types of function designs:</p>
<div id="coercingconverting-functions" class="section level3">
<h3>coercing/converting functions</h3>
<p>Functions like <code>to_factor()</code> or <code>to_label()</code>,
which convert variables into other types or add additional information
like variable or value labels as attribute, typically <em>return the
complete data frame</em> that was given as first argument <em>without
any new variables</em>. The variables specified in the
<code>...</code>-ellipses argument are converted (overwritten), all
other variables remain unchanged.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>x <span class="ot"><-</span> efc[, <span class="dv">3</span><span class="sc">:</span><span class="dv">5</span>]</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a></span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a>x <span class="sc">%>%</span> <span class="fu">str</span>()</span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> 'data.frame': 908 obs. of 3 variables:</span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> $ e16sex: num 2 2 2 2 2 2 1 2 2 2 ...</span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#> ..- attr(*, "label")= chr "elder's gender"</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#> ..- attr(*, "labels")= Named num [1:2] 1 2</span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> .. ..- attr(*, "names")= chr [1:2] "male" "female"</span></span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#> $ e17age: num 83 88 82 67 84 85 74 87 79 83 ...</span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#> ..- attr(*, "label")= chr "elder' age"</span></span>
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a><span class="co">#> $ e42dep: num 3 3 3 4 4 4 4 4 4 4 ...</span></span>
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="co">#> ..- attr(*, "label")= chr "elder's dependency"</span></span>
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a><span class="co">#> ..- attr(*, "labels")= Named num [1:4] 1 2 3 4</span></span>
<span id="cb3-14"><a href="#cb3-14" tabindex="-1"></a><span class="co">#> .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"</span></span>
<span id="cb3-15"><a href="#cb3-15" tabindex="-1"></a></span>
<span id="cb3-16"><a href="#cb3-16" tabindex="-1"></a><span class="fu">to_factor</span>(x, e42dep, e16sex) <span class="sc">%>%</span> <span class="fu">str</span>()</span>
<span id="cb3-17"><a href="#cb3-17" tabindex="-1"></a><span class="co">#> 'data.frame': 908 obs. of 3 variables:</span></span>
<span id="cb3-18"><a href="#cb3-18" tabindex="-1"></a><span class="co">#> $ e16sex: Factor w/ 2 levels "1","2": 2 2 2 2 2 2 1 2 2 2 ...</span></span>
<span id="cb3-19"><a href="#cb3-19" tabindex="-1"></a><span class="co">#> ..- attr(*, "labels")= Named num [1:2] 1 2</span></span>
<span id="cb3-20"><a href="#cb3-20" tabindex="-1"></a><span class="co">#> .. ..- attr(*, "names")= chr [1:2] "male" "female"</span></span>
<span id="cb3-21"><a href="#cb3-21" tabindex="-1"></a><span class="co">#> ..- attr(*, "label")= chr "elder's gender"</span></span>
<span id="cb3-22"><a href="#cb3-22" tabindex="-1"></a><span class="co">#> $ e17age: num 83 88 82 67 84 85 74 87 79 83 ...</span></span>
<span id="cb3-23"><a href="#cb3-23" tabindex="-1"></a><span class="co">#> ..- attr(*, "label")= chr "elder' age"</span></span>
<span id="cb3-24"><a href="#cb3-24" tabindex="-1"></a><span class="co">#> $ e42dep: Factor w/ 4 levels "1","2","3","4": 3 3 3 4 4 4 4 4 4 4 ...</span></span>
<span id="cb3-25"><a href="#cb3-25" tabindex="-1"></a><span class="co">#> ..- attr(*, "labels")= Named num [1:4] 1 2 3 4</span></span>
<span id="cb3-26"><a href="#cb3-26" tabindex="-1"></a><span class="co">#> .. ..- attr(*, "names")= chr [1:4] "independent" "slightly dependent" "moderately dependent" "severely dependent"</span></span>
<span id="cb3-27"><a href="#cb3-27" tabindex="-1"></a><span class="co">#> ..- attr(*, "label")= chr "elder's dependency"</span></span></code></pre></div>
</div>
<div id="transformationrecoding-functions" class="section level3">
<h3>transformation/recoding functions</h3>
<p>Functions like <code>rec()</code> or <code>dicho()</code>, which
transform or recode variables, by default add <em>the transformed or
recoded variables</em> to the data frame, so they return the new
variables <em>and</em> the original data as combined data frame. To
return <em>only the transformed and recoded variables</em> specified in
the <code>...</code>-ellipses argument, use argument
<code>append = FALSE</code>.</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="co"># complete data, including new columns</span></span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="fu">rec</span>(efc, c82cop1, c83cop2, <span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>, <span class="at">append =</span> <span class="cn">TRUE</span>) <span class="sc">%>%</span> <span class="fu">head</span>()</span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="co">#> c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4 c86cop5</span></span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="co">#> 1 16 2 2 83 3 3 2 2 2 1</span></span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> 2 148 2 2 88 3 3 3 3 3 4</span></span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#> 3 70 1 2 82 3 2 2 1 4 1</span></span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="co">#> 4 168 1 2 67 4 4 1 3 1 1</span></span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> 5 168 2 2 84 4 3 2 1 2 2</span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> 6 16 2 2 85 4 2 2 3 3 3</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> c87cop6 c88cop7 c89cop8 c90cop9 c160age c161sex c172code c175empl barthtot</span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> 1 1 2 3 3 56 2 2 1 75</span></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co">#> 2 1 3 2 2 54 2 2 1 75</span></span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#> 3 1 1 4 3 80 1 1 0 35</span></span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co">#> 4 1 1 2 4 69 1 2 0 0</span></span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#> 5 2 1 4 4 47 2 2 0 25</span></span>
<span id="cb4-16"><a href="#cb4-16" tabindex="-1"></a><span class="co">#> 6 2 2 1 1 56 1 2 1 60</span></span>
<span id="cb4-17"><a href="#cb4-17" tabindex="-1"></a><span class="co">#> neg_c_7 pos_v_4 quol_5 resttotn tot_sc_e n4pstu nur_pst c82cop1_r c83cop2_r</span></span>
<span id="cb4-18"><a href="#cb4-18" tabindex="-1"></a><span class="co">#> 1 12 12 14 0 4 0 NA 2 0</span></span>
<span id="cb4-19"><a href="#cb4-19" tabindex="-1"></a><span class="co">#> 2 20 11 10 4 0 0 NA 2 2</span></span>
<span id="cb4-20"><a href="#cb4-20" tabindex="-1"></a><span class="co">#> 3 11 13 7 0 1 2 2 0 0</span></span>
<span id="cb4-21"><a href="#cb4-21" tabindex="-1"></a><span class="co">#> 4 10 15 12 2 0 3 3 2 0</span></span>
<span id="cb4-22"><a href="#cb4-22" tabindex="-1"></a><span class="co">#> 5 12 15 19 2 1 2 2 2 0</span></span>
<span id="cb4-23"><a href="#cb4-23" tabindex="-1"></a><span class="co">#> 6 19 9 8 1 3 2 2 0 0</span></span>
<span id="cb4-24"><a href="#cb4-24" tabindex="-1"></a></span>
<span id="cb4-25"><a href="#cb4-25" tabindex="-1"></a><span class="co"># only new columns</span></span>
<span id="cb4-26"><a href="#cb4-26" tabindex="-1"></a><span class="fu">rec</span>(efc, c82cop1, c83cop2, <span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>, <span class="at">append =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> <span class="fu">head</span>()</span>
<span id="cb4-27"><a href="#cb4-27" tabindex="-1"></a><span class="co">#> c82cop1_r c83cop2_r</span></span>
<span id="cb4-28"><a href="#cb4-28" tabindex="-1"></a><span class="co">#> 1 2 0</span></span>
<span id="cb4-29"><a href="#cb4-29" tabindex="-1"></a><span class="co">#> 2 2 2</span></span>
<span id="cb4-30"><a href="#cb4-30" tabindex="-1"></a><span class="co">#> 3 0 0</span></span>
<span id="cb4-31"><a href="#cb4-31" tabindex="-1"></a><span class="co">#> 4 2 0</span></span>
<span id="cb4-32"><a href="#cb4-32" tabindex="-1"></a><span class="co">#> 5 2 0</span></span>
<span id="cb4-33"><a href="#cb4-33" tabindex="-1"></a><span class="co">#> 6 0 0</span></span></code></pre></div>
<p>These variables usually get a suffix, so you can bind these variables
as new columns to a data frame, for instance with
<code>add_columns()</code>. The function <code>add_columns()</code> is
useful if you want to bind/add columns within a pipe-chain <em>to the
end</em> of a data frame.</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>efc <span class="sc">%>%</span> </span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a> <span class="fu">rec</span>(c82cop1, c83cop2, <span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>, <span class="at">append =</span> <span class="cn">FALSE</span>) <span class="sc">%>%</span> </span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a> <span class="fu">add_columns</span>(efc) <span class="sc">%>%</span> </span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a> <span class="fu">head</span>()</span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a><span class="co">#> c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4 c86cop5</span></span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="co">#> 1 16 2 2 83 3 3 2 2 2 1</span></span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> 2 148 2 2 88 3 3 3 3 3 4</span></span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#> 3 70 1 2 82 3 2 2 1 4 1</span></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#> 4 168 1 2 67 4 4 1 3 1 1</span></span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="co">#> 5 168 2 2 84 4 3 2 1 2 2</span></span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#> 6 16 2 2 85 4 2 2 3 3 3</span></span>
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="co">#> c87cop6 c88cop7 c89cop8 c90cop9 c160age c161sex c172code c175empl barthtot</span></span>
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co">#> 1 1 2 3 3 56 2 2 1 75</span></span>
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="co">#> 2 1 3 2 2 54 2 2 1 75</span></span>
<span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a><span class="co">#> 3 1 1 4 3 80 1 1 0 35</span></span>
<span id="cb5-16"><a href="#cb5-16" tabindex="-1"></a><span class="co">#> 4 1 1 2 4 69 1 2 0 0</span></span>
<span id="cb5-17"><a href="#cb5-17" tabindex="-1"></a><span class="co">#> 5 2 1 4 4 47 2 2 0 25</span></span>
<span id="cb5-18"><a href="#cb5-18" tabindex="-1"></a><span class="co">#> 6 2 2 1 1 56 1 2 1 60</span></span>
<span id="cb5-19"><a href="#cb5-19" tabindex="-1"></a><span class="co">#> neg_c_7 pos_v_4 quol_5 resttotn tot_sc_e n4pstu nur_pst c82cop1_r c83cop2_r</span></span>
<span id="cb5-20"><a href="#cb5-20" tabindex="-1"></a><span class="co">#> 1 12 12 14 0 4 0 NA 2 0</span></span>
<span id="cb5-21"><a href="#cb5-21" tabindex="-1"></a><span class="co">#> 2 20 11 10 4 0 0 NA 2 2</span></span>
<span id="cb5-22"><a href="#cb5-22" tabindex="-1"></a><span class="co">#> 3 11 13 7 0 1 2 2 0 0</span></span>
<span id="cb5-23"><a href="#cb5-23" tabindex="-1"></a><span class="co">#> 4 10 15 12 2 0 3 3 2 0</span></span>
<span id="cb5-24"><a href="#cb5-24" tabindex="-1"></a><span class="co">#> 5 12 15 19 2 1 2 2 2 0</span></span>
<span id="cb5-25"><a href="#cb5-25" tabindex="-1"></a><span class="co">#> 6 19 9 8 1 3 2 2 0 0</span></span></code></pre></div>
<p>If <code>append = TRUE</code> and <code>suffix = ""</code>, recoded
variables will replace (overwrite) existing variables.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="co"># complete data, existing columns c82cop1 and c83cop2 are replaced</span></span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="fu">rec</span>(efc, c82cop1, c83cop2, <span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>, <span class="at">append =</span> <span class="cn">TRUE</span>, <span class="at">suffix =</span> <span class="st">""</span>) <span class="sc">%>%</span> <span class="fu">head</span>()</span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#> c12hour e15relat e16sex e17age e42dep c82cop1 c83cop2 c84cop3 c85cop4 c86cop5</span></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#> 1 16 2 2 83 3 2 0 2 2 1</span></span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co">#> 2 148 2 2 88 3 2 2 3 3 4</span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#> 3 70 1 2 82 3 0 0 1 4 1</span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#> 4 168 1 2 67 4 2 0 3 1 1</span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#> 5 168 2 2 84 4 2 0 1 2 2</span></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co">#> 6 16 2 2 85 4 0 0 3 3 3</span></span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#> c87cop6 c88cop7 c89cop8 c90cop9 c160age c161sex c172code c175empl barthtot</span></span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#> 1 1 2 3 3 56 2 2 1 75</span></span>
<span id="cb6-12"><a href="#cb6-12" tabindex="-1"></a><span class="co">#> 2 1 3 2 2 54 2 2 1 75</span></span>
<span id="cb6-13"><a href="#cb6-13" tabindex="-1"></a><span class="co">#> 3 1 1 4 3 80 1 1 0 35</span></span>
<span id="cb6-14"><a href="#cb6-14" tabindex="-1"></a><span class="co">#> 4 1 1 2 4 69 1 2 0 0</span></span>
<span id="cb6-15"><a href="#cb6-15" tabindex="-1"></a><span class="co">#> 5 2 1 4 4 47 2 2 0 25</span></span>
<span id="cb6-16"><a href="#cb6-16" tabindex="-1"></a><span class="co">#> 6 2 2 1 1 56 1 2 1 60</span></span>
<span id="cb6-17"><a href="#cb6-17" tabindex="-1"></a><span class="co">#> neg_c_7 pos_v_4 quol_5 resttotn tot_sc_e n4pstu nur_pst</span></span>
<span id="cb6-18"><a href="#cb6-18" tabindex="-1"></a><span class="co">#> 1 12 12 14 0 4 0 NA</span></span>
<span id="cb6-19"><a href="#cb6-19" tabindex="-1"></a><span class="co">#> 2 20 11 10 4 0 0 NA</span></span>
<span id="cb6-20"><a href="#cb6-20" tabindex="-1"></a><span class="co">#> 3 11 13 7 0 1 2 2</span></span>
<span id="cb6-21"><a href="#cb6-21" tabindex="-1"></a><span class="co">#> 4 10 15 12 2 0 3 3</span></span>
<span id="cb6-22"><a href="#cb6-22" tabindex="-1"></a><span class="co">#> 5 12 15 19 2 1 2 2</span></span>
<span id="cb6-23"><a href="#cb6-23" tabindex="-1"></a><span class="co">#> 6 19 9 8 1 3 2 2</span></span></code></pre></div>
</div>
</div>
<div id="sjmisc-and-dplyr" class="section level2">
<h2>sjmisc and dplyr</h2>
<p>The functions of <strong>sjmisc</strong> are designed to work
together seamlessly with other packages from the tidyverse, like
<strong>dplyr</strong>. For instance, you can use the functions from
<strong>sjmisc</strong> both within a pipe-worklflow to manipulate data
frames, or to create new variables with <code>mutate()</code>:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>efc <span class="sc">%>%</span> </span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a> <span class="fu">select</span>(c82cop1, c83cop2) <span class="sc">%>%</span> </span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a> <span class="fu">rec</span>(<span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>) <span class="sc">%>%</span> </span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a> <span class="fu">head</span>()</span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#> c82cop1 c83cop2 c82cop1_r c83cop2_r</span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#> 1 3 2 2 0</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#> 2 3 3 2 2</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#> 3 2 2 0 0</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#> 4 4 1 2 0</span></span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#> 5 3 2 2 0</span></span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co">#> 6 2 2 0 0</span></span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a></span>
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a>efc <span class="sc">%>%</span> </span>
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a> <span class="fu">select</span>(c82cop1, c83cop2) <span class="sc">%>%</span> </span>
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a> <span class="fu">mutate</span>(</span>
<span id="cb7-16"><a href="#cb7-16" tabindex="-1"></a> <span class="at">c82cop1_dicho =</span> <span class="fu">rec</span>(c82cop1, <span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>),</span>
<span id="cb7-17"><a href="#cb7-17" tabindex="-1"></a> <span class="at">c83cop2_dicho =</span> <span class="fu">rec</span>(c83cop2, <span class="at">rec =</span> <span class="st">"1,2=0; 3:4=2"</span>)</span>
<span id="cb7-18"><a href="#cb7-18" tabindex="-1"></a> ) <span class="sc">%>%</span> </span>
<span id="cb7-19"><a href="#cb7-19" tabindex="-1"></a> <span class="fu">head</span>()</span>
<span id="cb7-20"><a href="#cb7-20" tabindex="-1"></a><span class="co">#> c82cop1 c83cop2 c82cop1_dicho c83cop2_dicho</span></span>
<span id="cb7-21"><a href="#cb7-21" tabindex="-1"></a><span class="co">#> 1 3 2 2 0</span></span>
<span id="cb7-22"><a href="#cb7-22" tabindex="-1"></a><span class="co">#> 2 3 3 2 2</span></span>
<span id="cb7-23"><a href="#cb7-23" tabindex="-1"></a><span class="co">#> 3 2 2 0 0</span></span>
<span id="cb7-24"><a href="#cb7-24" tabindex="-1"></a><span class="co">#> 4 4 1 2 0</span></span>
<span id="cb7-25"><a href="#cb7-25" tabindex="-1"></a><span class="co">#> 5 3 2 2 0</span></span>
<span id="cb7-26"><a href="#cb7-26" tabindex="-1"></a><span class="co">#> 6 2 2 0 0</span></span></code></pre></div>
<p>This makes it easy to adapt the <strong>sjmisc</strong> functions to
your own workflow.</p>
</div>
</div>
<!-- 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>
|