1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
|
<!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>Recoding Variables</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">Recoding Variables</h1>
<h4 class="author">Daniel Lüdecke</h4>
<h4 class="date">2024-05-13</h4>
<p>Data preparation is a common task in research, which usually takes
the most amount of time in the analytical process.
<strong>sjmisc</strong> is a package with special focus on
transformation of <em>variables</em> that fits into the workflow and
design-philosophy of the so-called “tidyverse”.</p>
<p>Basically, this package complements the <strong>dplyr</strong>
package in that <strong>sjmisc</strong> takes over data transformation
tasks on variables, like recoding, dichotomizing or grouping variables,
setting and replacing missing values, etc. A distinctive feature of
<strong>sjmisc</strong> is the support for labelled data, which is
especially useful for users who often work with data sets from other
statistical software packages like <em>SPSS</em> or <em>Stata</em>.</p>
<p>This vignette demonstrate some of the important recoding-functions in
<strong>sjmisc</strong>. The examples are based on data from the
EUROFAMCARE project, a survey on the situation of family carers of older
people in Europe. The sample data set <code>efc</code> is part of this
package.</p>
<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></code></pre></div>
<p>To show the results after recoding variables, the <code>frq()</code>
function is used to print frequency tables.</p>
<div id="dichotomization-dividing-variables-into-two-groups" class="section level2">
<h2>Dichotomization: dividing variables into two groups</h2>
<p><code>dicho()</code> dichotomizes variables into “dummy” variables
(with 0/1 coding). Dichotomization is either done by median, mean or a
specific value (see argument <code>dich.by</code>).</p>
<p>Like all recoding-functions in <strong>sjmisc</strong>,
<code>dicho()</code> returns the complete data frame <em>including</em>
the recoded variables, if the first argument is a
<code>data.frame</code>. If the first argument is a vector, only the
recoded variable is returned. See <a href="design_philosophy.html">this
vignette</a> for details about the function-design.</p>
<p>If <code>dicho()</code> returns a data frame, the recoded variables
have the same name as the original variable, including a suffix
<code>_d</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"># age, ranged from 65 to 104, in this output</span></span>
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="co"># grouped to get a shorter table</span></span>
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a><span class="fu">frq</span>(efc, e17age, <span class="at">auto.grp =</span> <span class="dv">5</span>)</span>
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a><span class="co">#> elder' age (e17age) <numeric> </span></span>
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=79.12 sd=8.09</span></span>
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a><span class="co">#> -----------------------------------------------</span></span>
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a><span class="co">#> 1 | 65-72 | 212 | 23.35 | 23.79 | 23.79</span></span>
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a><span class="co">#> 2 | 73-80 | 277 | 30.51 | 31.09 | 54.88</span></span>
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a><span class="co">#> 3 | 81-88 | 270 | 29.74 | 30.30 | 85.19</span></span>
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a><span class="co">#> 4 | 89-96 | 124 | 13.66 | 13.92 | 99.10</span></span>
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a><span class="co">#> 5 | 97-104 | 8 | 0.88 | 0.90 | 100.00</span></span>
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 17 | 1.87 | <NA> | <NA></span></span>
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a></span>
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a><span class="co"># splitting is done at the median by default:</span></span>
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a><span class="fu">median</span>(efc<span class="sc">$</span>e17age, <span class="at">na.rm =</span> <span class="cn">TRUE</span>)</span>
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a><span class="co">#> [1] 79</span></span>
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a></span>
<span id="cb2-20"><a href="#cb2-20" tabindex="-1"></a><span class="co"># the recoded variable is now named "e17age_d"</span></span>
<span id="cb2-21"><a href="#cb2-21" tabindex="-1"></a>efc <span class="ot"><-</span> <span class="fu">dicho</span>(efc, e17age)</span>
<span id="cb2-22"><a href="#cb2-22" tabindex="-1"></a><span class="fu">frq</span>(efc, e17age_d)</span>
<span id="cb2-23"><a href="#cb2-23" tabindex="-1"></a><span class="co">#> elder' age (e17age_d) <categorical> </span></span>
<span id="cb2-24"><a href="#cb2-24" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=0.49 sd=0.50</span></span>
<span id="cb2-25"><a href="#cb2-25" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb2-26"><a href="#cb2-26" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb2-27"><a href="#cb2-27" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb2-28"><a href="#cb2-28" tabindex="-1"></a><span class="co">#> 0 | 455 | 50.11 | 51.07 | 51.07</span></span>
<span id="cb2-29"><a href="#cb2-29" tabindex="-1"></a><span class="co">#> 1 | 436 | 48.02 | 48.93 | 100.00</span></span>
<span id="cb2-30"><a href="#cb2-30" tabindex="-1"></a><span class="co">#> <NA> | 17 | 1.87 | <NA> | <NA></span></span></code></pre></div>
<p>As <code>dicho()</code>, like all recoding-functions, supports <a href="https://cran.r-project.org/package=sjlabelled">labelled data</a>,
the variable preserves it variable label (but not the value labels). You
can directly define value labels inside the function:</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> <span class="fu">dicho</span>(efc<span class="sc">$</span>e17age, <span class="at">val.labels =</span> <span class="fu">c</span>(<span class="st">"young age"</span>, <span class="st">"old age"</span>))</span>
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="fu">frq</span>(x)</span>
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> elder' age (x) <categorical> </span></span>
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=0.49 sd=0.50</span></span>
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#> --------------------------------------------------</span></span>
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> 0 | young age | 455 | 50.11 | 51.07 | 51.07</span></span>
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a><span class="co">#> 1 | old age | 436 | 48.02 | 48.93 | 100.00</span></span>
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 17 | 1.87 | <NA> | <NA></span></span></code></pre></div>
<p>To split a variable at a different value, use the
<code>dich.by</code>-argument. The value specified in
<code>dich.by</code> is <em>inclusive</em>, i.e. all values from lowest
to and including <code>dich.by</code> are recoded into the lower
category, while all values <em>above</em> <code>dich.by</code> are
recoded into the higher category.</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"># split at upper quartile</span></span>
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">dicho</span>(</span>
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a> efc<span class="sc">$</span>e17age, </span>
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a> <span class="at">dich.by =</span> <span class="fu">quantile</span>(efc<span class="sc">$</span>e17age, <span class="at">probs =</span> .<span class="dv">75</span>, <span class="at">na.rm =</span> <span class="cn">TRUE</span>), </span>
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a> <span class="at">val.labels =</span> <span class="fu">c</span>(<span class="st">"younger three quarters"</span>, <span class="st">"oldest quarter"</span>)</span>
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a>)</span>
<span id="cb4-7"><a href="#cb4-7" tabindex="-1"></a><span class="fu">frq</span>(x)</span>
<span id="cb4-8"><a href="#cb4-8" tabindex="-1"></a><span class="co">#> elder' age (x) <categorical> </span></span>
<span id="cb4-9"><a href="#cb4-9" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=0.24 sd=0.43</span></span>
<span id="cb4-10"><a href="#cb4-10" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb4-11"><a href="#cb4-11" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb4-12"><a href="#cb4-12" tabindex="-1"></a><span class="co">#> ---------------------------------------------------------------</span></span>
<span id="cb4-13"><a href="#cb4-13" tabindex="-1"></a><span class="co">#> 0 | younger three quarters | 678 | 74.67 | 76.09 | 76.09</span></span>
<span id="cb4-14"><a href="#cb4-14" tabindex="-1"></a><span class="co">#> 1 | oldest quarter | 213 | 23.46 | 23.91 | 100.00</span></span>
<span id="cb4-15"><a href="#cb4-15" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 17 | 1.87 | <NA> | <NA></span></span></code></pre></div>
<p>Since the distribution of values in a dataset may differ for
different subgroups, all recoding-functions also work on grouped data
frames. In the following example, first, the age-variable
<code>e17age</code> is dichotomized at the median. Then, the data is
grouped by gender (<code>c161sex</code>) and the dichotomization is done
for each subgroup, i.e. it once relates to the median age in the
subgroup of female, and once to the median age in the subgroup of male
family carers.</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><span class="fu">data</span>(efc)</span>
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a>x1 <span class="ot"><-</span> <span class="fu">dicho</span>(efc<span class="sc">$</span>e17age)</span>
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a>x2 <span class="ot"><-</span> efc <span class="sc">%>%</span> </span>
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">group_by</span>(c161sex) <span class="sc">%>%</span> </span>
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a> <span class="fu">dicho</span>(e17age) <span class="sc">%>%</span> </span>
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">pull</span>(e17age_d)</span>
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a></span>
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co"># median age of total sample</span></span>
<span id="cb5-10"><a href="#cb5-10" tabindex="-1"></a><span class="fu">frq</span>(x1)</span>
<span id="cb5-11"><a href="#cb5-11" tabindex="-1"></a><span class="co">#> elder' age (x) <categorical> </span></span>
<span id="cb5-12"><a href="#cb5-12" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=0.49 sd=0.50</span></span>
<span id="cb5-13"><a href="#cb5-13" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb5-14"><a href="#cb5-14" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb5-15"><a href="#cb5-15" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb5-16"><a href="#cb5-16" tabindex="-1"></a><span class="co">#> 0 | 455 | 50.11 | 51.07 | 51.07</span></span>
<span id="cb5-17"><a href="#cb5-17" tabindex="-1"></a><span class="co">#> 1 | 436 | 48.02 | 48.93 | 100.00</span></span>
<span id="cb5-18"><a href="#cb5-18" tabindex="-1"></a><span class="co">#> <NA> | 17 | 1.87 | <NA> | <NA></span></span>
<span id="cb5-19"><a href="#cb5-19" tabindex="-1"></a></span>
<span id="cb5-20"><a href="#cb5-20" tabindex="-1"></a><span class="co"># median age of total sample, with median-split applied</span></span>
<span id="cb5-21"><a href="#cb5-21" tabindex="-1"></a><span class="co"># to distribution of age by subgroups of gender</span></span>
<span id="cb5-22"><a href="#cb5-22" tabindex="-1"></a><span class="fu">frq</span>(x2)</span>
<span id="cb5-23"><a href="#cb5-23" tabindex="-1"></a><span class="co">#> elder' age (x) <numeric> </span></span>
<span id="cb5-24"><a href="#cb5-24" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=1.50 sd=0.50</span></span>
<span id="cb5-25"><a href="#cb5-25" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb5-26"><a href="#cb5-26" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb5-27"><a href="#cb5-27" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb5-28"><a href="#cb5-28" tabindex="-1"></a><span class="co">#> 1 | 449 | 49.45 | 50.39 | 50.39</span></span>
<span id="cb5-29"><a href="#cb5-29" tabindex="-1"></a><span class="co">#> 2 | 442 | 48.68 | 49.61 | 100.00</span></span>
<span id="cb5-30"><a href="#cb5-30" tabindex="-1"></a><span class="co">#> <NA> | 17 | 1.87 | <NA> | <NA></span></span></code></pre></div>
</div>
<div id="splitting-variables-into-several-groups" class="section level2">
<h2>Splitting variables into several groups</h2>
<p><code>split_var()</code> recodes numeric variables into equal sized
groups, i.e. a variable is cut into a smaller number of groups at
specific cut points. The amount of groups depends on the
<code>n</code>-argument and cuts a variable into <code>n</code>
quantiles.</p>
<p>Similar to <code>dicho()</code>, if the first argument in
<code>split_var()</code> is a data frame, the complete data frame
including the new recoded variable(s), with suffix <code>_g</code>, is
returned.</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>x <span class="ot"><-</span> <span class="fu">split_var</span>(efc<span class="sc">$</span>e17age, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="fu">frq</span>(x)</span>
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#> elder' age (x) <categorical> </span></span>
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=2.05 sd=0.82</span></span>
<span id="cb6-5"><a href="#cb6-5" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb6-6"><a href="#cb6-6" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb6-7"><a href="#cb6-7" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb6-8"><a href="#cb6-8" tabindex="-1"></a><span class="co">#> 1 | 274 | 30.18 | 30.75 | 30.75</span></span>
<span id="cb6-9"><a href="#cb6-9" tabindex="-1"></a><span class="co">#> 2 | 294 | 32.38 | 33.00 | 63.75</span></span>
<span id="cb6-10"><a href="#cb6-10" tabindex="-1"></a><span class="co">#> 3 | 323 | 35.57 | 36.25 | 100.00</span></span>
<span id="cb6-11"><a href="#cb6-11" tabindex="-1"></a><span class="co">#> <NA> | 17 | 1.87 | <NA> | <NA></span></span></code></pre></div>
<p>Unlike dplyr’s <code>ntile()</code>, <code>split_var()</code> never
splits a value into two different categories, i.e. you always get a
“clean” separation of original categories. In other words: cases that
have identical values in a variable will always be recoded into the same
group. The following example demonstrates the differences:</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>x <span class="ot"><-</span> dplyr<span class="sc">::</span><span class="fu">ntile</span>(efc<span class="sc">$</span>neg_c_7, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="co"># for some cases, value "10" is recoded into category "1",</span></span>
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a><span class="co"># for other cases into category "2". Same is true for value "13"</span></span>
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a><span class="fu">table</span>(efc<span class="sc">$</span>neg_c_7, x)</span>
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a><span class="co">#> x</span></span>
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a><span class="co">#> 1 2 3</span></span>
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a><span class="co">#> 7 75 0 0</span></span>
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="co">#> 8 99 0 0</span></span>
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#> 9 106 0 0</span></span>
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#> 10 18 102 0</span></span>
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co">#> 11 0 96 0</span></span>
<span id="cb7-12"><a href="#cb7-12" tabindex="-1"></a><span class="co">#> 12 0 85 0</span></span>
<span id="cb7-13"><a href="#cb7-13" tabindex="-1"></a><span class="co">#> 13 0 14 50</span></span>
<span id="cb7-14"><a href="#cb7-14" tabindex="-1"></a><span class="co">#> 14 0 0 54</span></span>
<span id="cb7-15"><a href="#cb7-15" tabindex="-1"></a><span class="co">#> 15 0 0 45</span></span>
<span id="cb7-16"><a href="#cb7-16" tabindex="-1"></a><span class="co">#> 16 0 0 30</span></span>
<span id="cb7-17"><a href="#cb7-17" tabindex="-1"></a><span class="co">#> 17 0 0 35</span></span>
<span id="cb7-18"><a href="#cb7-18" tabindex="-1"></a><span class="co">#> 18 0 0 26</span></span>
<span id="cb7-19"><a href="#cb7-19" tabindex="-1"></a><span class="co">#> 19 0 0 16</span></span>
<span id="cb7-20"><a href="#cb7-20" tabindex="-1"></a><span class="co">#> 20 0 0 16</span></span>
<span id="cb7-21"><a href="#cb7-21" tabindex="-1"></a><span class="co">#> 21 0 0 2</span></span>
<span id="cb7-22"><a href="#cb7-22" tabindex="-1"></a><span class="co">#> 22 0 0 7</span></span>
<span id="cb7-23"><a href="#cb7-23" tabindex="-1"></a><span class="co">#> 23 0 0 4</span></span>
<span id="cb7-24"><a href="#cb7-24" tabindex="-1"></a><span class="co">#> 24 0 0 3</span></span>
<span id="cb7-25"><a href="#cb7-25" tabindex="-1"></a><span class="co">#> 25 0 0 6</span></span>
<span id="cb7-26"><a href="#cb7-26" tabindex="-1"></a><span class="co">#> 27 0 0 1</span></span>
<span id="cb7-27"><a href="#cb7-27" tabindex="-1"></a><span class="co">#> 28 0 0 2</span></span>
<span id="cb7-28"><a href="#cb7-28" tabindex="-1"></a></span>
<span id="cb7-29"><a href="#cb7-29" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">split_var</span>(efc<span class="sc">$</span>neg_c_7, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb7-30"><a href="#cb7-30" tabindex="-1"></a><span class="co"># no separation of cases with identical values.</span></span>
<span id="cb7-31"><a href="#cb7-31" tabindex="-1"></a><span class="fu">table</span>(efc<span class="sc">$</span>neg_c_7, x)</span>
<span id="cb7-32"><a href="#cb7-32" tabindex="-1"></a><span class="co">#> x</span></span>
<span id="cb7-33"><a href="#cb7-33" tabindex="-1"></a><span class="co">#> 1 2 3</span></span>
<span id="cb7-34"><a href="#cb7-34" tabindex="-1"></a><span class="co">#> 7 75 0 0</span></span>
<span id="cb7-35"><a href="#cb7-35" tabindex="-1"></a><span class="co">#> 8 99 0 0</span></span>
<span id="cb7-36"><a href="#cb7-36" tabindex="-1"></a><span class="co">#> 9 106 0 0</span></span>
<span id="cb7-37"><a href="#cb7-37" tabindex="-1"></a><span class="co">#> 10 0 120 0</span></span>
<span id="cb7-38"><a href="#cb7-38" tabindex="-1"></a><span class="co">#> 11 0 96 0</span></span>
<span id="cb7-39"><a href="#cb7-39" tabindex="-1"></a><span class="co">#> 12 0 85 0</span></span>
<span id="cb7-40"><a href="#cb7-40" tabindex="-1"></a><span class="co">#> 13 0 0 64</span></span>
<span id="cb7-41"><a href="#cb7-41" tabindex="-1"></a><span class="co">#> 14 0 0 54</span></span>
<span id="cb7-42"><a href="#cb7-42" tabindex="-1"></a><span class="co">#> 15 0 0 45</span></span>
<span id="cb7-43"><a href="#cb7-43" tabindex="-1"></a><span class="co">#> 16 0 0 30</span></span>
<span id="cb7-44"><a href="#cb7-44" tabindex="-1"></a><span class="co">#> 17 0 0 35</span></span>
<span id="cb7-45"><a href="#cb7-45" tabindex="-1"></a><span class="co">#> 18 0 0 26</span></span>
<span id="cb7-46"><a href="#cb7-46" tabindex="-1"></a><span class="co">#> 19 0 0 16</span></span>
<span id="cb7-47"><a href="#cb7-47" tabindex="-1"></a><span class="co">#> 20 0 0 16</span></span>
<span id="cb7-48"><a href="#cb7-48" tabindex="-1"></a><span class="co">#> 21 0 0 2</span></span>
<span id="cb7-49"><a href="#cb7-49" tabindex="-1"></a><span class="co">#> 22 0 0 7</span></span>
<span id="cb7-50"><a href="#cb7-50" tabindex="-1"></a><span class="co">#> 23 0 0 4</span></span>
<span id="cb7-51"><a href="#cb7-51" tabindex="-1"></a><span class="co">#> 24 0 0 3</span></span>
<span id="cb7-52"><a href="#cb7-52" tabindex="-1"></a><span class="co">#> 25 0 0 6</span></span>
<span id="cb7-53"><a href="#cb7-53" tabindex="-1"></a><span class="co">#> 27 0 0 1</span></span>
<span id="cb7-54"><a href="#cb7-54" tabindex="-1"></a><span class="co">#> 28 0 0 2</span></span></code></pre></div>
<p><code>split_var()</code>, unlike <code>ntile()</code>, does therefor
not always return exactly equal-sized groups:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>x <span class="ot"><-</span> dplyr<span class="sc">::</span><span class="fu">ntile</span>(efc<span class="sc">$</span>neg_c_7, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a><span class="fu">frq</span>(x)</span>
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a><span class="co">#> x <integer> </span></span>
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a><span class="co">#> # total N=908 valid N=892 mean=2.00 sd=0.82</span></span>
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb8-7"><a href="#cb8-7" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb8-8"><a href="#cb8-8" tabindex="-1"></a><span class="co">#> 1 | 298 | 32.82 | 33.41 | 33.41</span></span>
<span id="cb8-9"><a href="#cb8-9" tabindex="-1"></a><span class="co">#> 2 | 297 | 32.71 | 33.30 | 66.70</span></span>
<span id="cb8-10"><a href="#cb8-10" tabindex="-1"></a><span class="co">#> 3 | 297 | 32.71 | 33.30 | 100.00</span></span>
<span id="cb8-11"><a href="#cb8-11" tabindex="-1"></a><span class="co">#> <NA> | 16 | 1.76 | <NA> | <NA></span></span>
<span id="cb8-12"><a href="#cb8-12" tabindex="-1"></a></span>
<span id="cb8-13"><a href="#cb8-13" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">split_var</span>(efc<span class="sc">$</span>neg_c_7, <span class="at">n =</span> <span class="dv">3</span>)</span>
<span id="cb8-14"><a href="#cb8-14" tabindex="-1"></a><span class="fu">frq</span>(x)</span>
<span id="cb8-15"><a href="#cb8-15" tabindex="-1"></a><span class="co">#> Negative impact with 7 items (x) <categorical> </span></span>
<span id="cb8-16"><a href="#cb8-16" tabindex="-1"></a><span class="co">#> # total N=908 valid N=892 mean=2.03 sd=0.81</span></span>
<span id="cb8-17"><a href="#cb8-17" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb8-18"><a href="#cb8-18" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb8-19"><a href="#cb8-19" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb8-20"><a href="#cb8-20" tabindex="-1"></a><span class="co">#> 1 | 280 | 30.84 | 31.39 | 31.39</span></span>
<span id="cb8-21"><a href="#cb8-21" tabindex="-1"></a><span class="co">#> 2 | 301 | 33.15 | 33.74 | 65.13</span></span>
<span id="cb8-22"><a href="#cb8-22" tabindex="-1"></a><span class="co">#> 3 | 311 | 34.25 | 34.87 | 100.00</span></span>
<span id="cb8-23"><a href="#cb8-23" tabindex="-1"></a><span class="co">#> <NA> | 16 | 1.76 | <NA> | <NA></span></span></code></pre></div>
</div>
<div id="recode-variables-into-equal-ranged-groups" class="section level2">
<h2>Recode variables into equal-ranged groups</h2>
<p>With <code>group_var()</code>, variables can be grouped into equal
ranged categories, i.e. a variable is cut into a smaller number of
groups, where each group has the same value range.
<code>group_labels()</code> creates the related value labels.</p>
<p>The range of the groups is defined in the <code>size</code>-argument.
At the same time, the <code>size</code>-argument also defines the
<em>lower bound</em> of one of the groups.</p>
<p>For instance, if the lowest value of a variable is 1 and the maximum
is 10, and <code>size = 5</code>, then</p>
<ol style="list-style-type: lower-alpha">
<li>each group will have a range of 5, and</li>
<li>one of the groups will start with the value 5.</li>
</ol>
<p>This means, that an equal-ranged grouping will define groups from
<em>0 to 4</em>, <em>5 to 9</em> and <em>10-14</em>. Each of these
groups has a range of 5, and one of the groups starts with the value
5.</p>
<p>The group assignment becomes clearer, when
<code>group_labels()</code> is used in parallel:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="fu">set.seed</span>(<span class="dv">123</span>)</span>
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">round</span>(<span class="fu">runif</span>(<span class="at">n =</span> <span class="dv">150</span>, <span class="dv">1</span>, <span class="dv">10</span>))</span>
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a></span>
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a><span class="fu">frq</span>(x)</span>
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a><span class="co">#> x <numeric> </span></span>
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=5.52 sd=2.63</span></span>
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a><span class="co">#> -------------------------------------</span></span>
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a><span class="co">#> 1 | 6 | 4.00 | 4.00 | 4.00</span></span>
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a><span class="co">#> 2 | 19 | 12.67 | 12.67 | 16.67</span></span>
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a><span class="co">#> 3 | 16 | 10.67 | 10.67 | 27.33</span></span>
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a><span class="co">#> 4 | 17 | 11.33 | 11.33 | 38.67</span></span>
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a><span class="co">#> 5 | 20 | 13.33 | 13.33 | 52.00</span></span>
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a><span class="co">#> 6 | 12 | 8.00 | 8.00 | 60.00</span></span>
<span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a><span class="co">#> 7 | 19 | 12.67 | 12.67 | 72.67</span></span>
<span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a><span class="co">#> 8 | 16 | 10.67 | 10.67 | 83.33</span></span>
<span id="cb9-18"><a href="#cb9-18" tabindex="-1"></a><span class="co">#> 9 | 15 | 10.00 | 10.00 | 93.33</span></span>
<span id="cb9-19"><a href="#cb9-19" tabindex="-1"></a><span class="co">#> 10 | 10 | 6.67 | 6.67 | 100.00</span></span>
<span id="cb9-20"><a href="#cb9-20" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span>
<span id="cb9-21"><a href="#cb9-21" tabindex="-1"></a></span>
<span id="cb9-22"><a href="#cb9-22" tabindex="-1"></a><span class="fu">frq</span>(<span class="fu">group_var</span>(x, <span class="at">size =</span> <span class="dv">5</span>))</span>
<span id="cb9-23"><a href="#cb9-23" tabindex="-1"></a><span class="co">#> x <numeric> </span></span>
<span id="cb9-24"><a href="#cb9-24" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=1.68 sd=0.59</span></span>
<span id="cb9-25"><a href="#cb9-25" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb9-26"><a href="#cb9-26" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb9-27"><a href="#cb9-27" tabindex="-1"></a><span class="co">#> -------------------------------------</span></span>
<span id="cb9-28"><a href="#cb9-28" tabindex="-1"></a><span class="co">#> 1 | 58 | 38.67 | 38.67 | 38.67</span></span>
<span id="cb9-29"><a href="#cb9-29" tabindex="-1"></a><span class="co">#> 2 | 82 | 54.67 | 54.67 | 93.33</span></span>
<span id="cb9-30"><a href="#cb9-30" tabindex="-1"></a><span class="co">#> 3 | 10 | 6.67 | 6.67 | 100.00</span></span>
<span id="cb9-31"><a href="#cb9-31" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span>
<span id="cb9-32"><a href="#cb9-32" tabindex="-1"></a></span>
<span id="cb9-33"><a href="#cb9-33" tabindex="-1"></a><span class="fu">group_labels</span>(x, <span class="at">size =</span> <span class="dv">5</span>)</span>
<span id="cb9-34"><a href="#cb9-34" tabindex="-1"></a><span class="co">#> [1] "0-4" "5-9" "10-14"</span></span>
<span id="cb9-35"><a href="#cb9-35" tabindex="-1"></a></span>
<span id="cb9-36"><a href="#cb9-36" tabindex="-1"></a>dummy <span class="ot"><-</span> <span class="fu">group_var</span>(x, <span class="at">size =</span> <span class="dv">5</span>, <span class="at">as.num =</span> <span class="cn">FALSE</span>)</span>
<span id="cb9-37"><a href="#cb9-37" tabindex="-1"></a><span class="fu">levels</span>(dummy) <span class="ot"><-</span> <span class="fu">group_labels</span>(x, <span class="at">size =</span> <span class="dv">5</span>)</span>
<span id="cb9-38"><a href="#cb9-38" tabindex="-1"></a><span class="fu">frq</span>(dummy)</span>
<span id="cb9-39"><a href="#cb9-39" tabindex="-1"></a><span class="co">#> x <categorical> </span></span>
<span id="cb9-40"><a href="#cb9-40" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=1.68 sd=0.59</span></span>
<span id="cb9-41"><a href="#cb9-41" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb9-42"><a href="#cb9-42" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb9-43"><a href="#cb9-43" tabindex="-1"></a><span class="co">#> -------------------------------------</span></span>
<span id="cb9-44"><a href="#cb9-44" tabindex="-1"></a><span class="co">#> 0-4 | 58 | 38.67 | 38.67 | 38.67</span></span>
<span id="cb9-45"><a href="#cb9-45" tabindex="-1"></a><span class="co">#> 5-9 | 82 | 54.67 | 54.67 | 93.33</span></span>
<span id="cb9-46"><a href="#cb9-46" tabindex="-1"></a><span class="co">#> 10-14 | 10 | 6.67 | 6.67 | 100.00</span></span>
<span id="cb9-47"><a href="#cb9-47" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span>
<span id="cb9-48"><a href="#cb9-48" tabindex="-1"></a></span>
<span id="cb9-49"><a href="#cb9-49" tabindex="-1"></a>dummy <span class="ot"><-</span> <span class="fu">group_var</span>(x, <span class="at">size =</span> <span class="dv">3</span>, <span class="at">as.num =</span> <span class="cn">FALSE</span>)</span>
<span id="cb9-50"><a href="#cb9-50" tabindex="-1"></a><span class="fu">levels</span>(dummy) <span class="ot"><-</span> <span class="fu">group_labels</span>(x, <span class="at">size =</span> <span class="dv">3</span>)</span>
<span id="cb9-51"><a href="#cb9-51" tabindex="-1"></a><span class="fu">frq</span>(dummy)</span>
<span id="cb9-52"><a href="#cb9-52" tabindex="-1"></a><span class="co">#> x <categorical> </span></span>
<span id="cb9-53"><a href="#cb9-53" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=2.48 sd=0.96</span></span>
<span id="cb9-54"><a href="#cb9-54" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb9-55"><a href="#cb9-55" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb9-56"><a href="#cb9-56" tabindex="-1"></a><span class="co">#> -------------------------------------</span></span>
<span id="cb9-57"><a href="#cb9-57" tabindex="-1"></a><span class="co">#> 0-2 | 25 | 16.67 | 16.67 | 16.67</span></span>
<span id="cb9-58"><a href="#cb9-58" tabindex="-1"></a><span class="co">#> 3-5 | 53 | 35.33 | 35.33 | 52.00</span></span>
<span id="cb9-59"><a href="#cb9-59" tabindex="-1"></a><span class="co">#> 6-8 | 47 | 31.33 | 31.33 | 83.33</span></span>
<span id="cb9-60"><a href="#cb9-60" tabindex="-1"></a><span class="co">#> 9-11 | 25 | 16.67 | 16.67 | 100.00</span></span>
<span id="cb9-61"><a href="#cb9-61" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span></code></pre></div>
<p>The argument <code>right.interval</code> can be used when
<code>size</code> should indicate the <em>upper bound</em> of a
group-range.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>dummy <span class="ot"><-</span> <span class="fu">group_var</span>(x, <span class="at">size =</span> <span class="dv">4</span>, <span class="at">as.num =</span> <span class="cn">FALSE</span>)</span>
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a><span class="fu">levels</span>(dummy) <span class="ot"><-</span> <span class="fu">group_labels</span>(x, <span class="at">size =</span> <span class="dv">4</span>)</span>
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a><span class="fu">frq</span>(dummy)</span>
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a><span class="co">#> x <categorical> </span></span>
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=2.00 sd=0.74</span></span>
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#> -------------------------------------</span></span>
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#> 0-3 | 41 | 27.33 | 27.33 | 27.33</span></span>
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#> 4-7 | 68 | 45.33 | 45.33 | 72.67</span></span>
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#> 8-11 | 41 | 27.33 | 27.33 | 100.00</span></span>
<span id="cb10-12"><a href="#cb10-12" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span>
<span id="cb10-13"><a href="#cb10-13" tabindex="-1"></a></span>
<span id="cb10-14"><a href="#cb10-14" tabindex="-1"></a>dummy <span class="ot"><-</span> <span class="fu">group_var</span>(x, <span class="at">size =</span> <span class="dv">4</span>, <span class="at">as.num =</span> <span class="cn">FALSE</span>, <span class="at">right.interval =</span> <span class="cn">TRUE</span>)</span>
<span id="cb10-15"><a href="#cb10-15" tabindex="-1"></a><span class="fu">levels</span>(dummy) <span class="ot"><-</span> <span class="fu">group_labels</span>(x, <span class="at">size =</span> <span class="dv">4</span>, <span class="at">right.interval =</span> <span class="cn">TRUE</span>)</span>
<span id="cb10-16"><a href="#cb10-16" tabindex="-1"></a><span class="fu">frq</span>(dummy)</span>
<span id="cb10-17"><a href="#cb10-17" tabindex="-1"></a><span class="co">#> x <categorical> </span></span>
<span id="cb10-18"><a href="#cb10-18" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=1.78 sd=0.71</span></span>
<span id="cb10-19"><a href="#cb10-19" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb10-20"><a href="#cb10-20" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb10-21"><a href="#cb10-21" tabindex="-1"></a><span class="co">#> -------------------------------------</span></span>
<span id="cb10-22"><a href="#cb10-22" tabindex="-1"></a><span class="co">#> 1-4 | 58 | 38.67 | 38.67 | 38.67</span></span>
<span id="cb10-23"><a href="#cb10-23" tabindex="-1"></a><span class="co">#> 5-8 | 67 | 44.67 | 44.67 | 83.33</span></span>
<span id="cb10-24"><a href="#cb10-24" tabindex="-1"></a><span class="co">#> 9-12 | 25 | 16.67 | 16.67 | 100.00</span></span>
<span id="cb10-25"><a href="#cb10-25" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span></code></pre></div>
</div>
<div id="flexible-recoding-of-variables" class="section level2">
<h2>Flexible recoding of variables</h2>
<p><code>rec()</code> recodes old values of variables into new values,
and can be considered as a “classical” recode-function. The
recode-pattern, i.e. which new values should replace the old values, is
defined in the <code>rec</code>-argument. This argument has a specific
“syntax”:</p>
<ul>
<li><p><strong>recode pairs</strong>: Each recode pair has to be
separated by a ;, e.g. <code>rec = "1=1; 2=4; 3=2; 4=3"</code></p></li>
<li><p><strong>multiple values</strong>: Multiple old values that should
be recoded into a new single value may be separated with comma,
e.g. <code>rec = "1,2=1; 3,4=2"</code></p></li>
<li><p><strong>value range</strong>: A value range is indicated by a
colon, e.g. <code>rec = "1:4=1; 5:8=2"</code> (recodes all values from 1
to 4 into 1, and from 5 to 8 into 2)</p></li>
<li><p><strong>value range for doubles</strong>: For double vectors
(with fractional part), all values within the specified range are
recoded; e.g. <code>rec = "1:2.5=1;2.6:3=2"</code> recodes 1 to 2.5 into
1 and 2.6 to 3 into 2, but 2.55 would not be recoded (since it’s not
included in any of the specified ranges)</p></li>
<li><p><strong>“min” and “max”</strong>: Minimum and maximum values are
indicates by <code>min</code> (or <code>lo</code>) and <code>max</code>
(or <code>hi</code>), e.g. <code>rec = "min:4=1; 5:max=2"</code>
(recodes all values from minimum values of x to 4 into 1, and from 5 to
maximum values of x into 2) You can also use <code>min</code> or
<code>max</code> to recode a value into the minimum or maximum value of
a variable, e.g. <code>rec = "min:4=1; 5:7=max"</code> (recodes all
values from minimum values of x to 4 into 1, and from 5 to 7 into the
maximum value of x).</p></li>
<li><p><strong>“else”</strong>: All other values, which have not been
specified yet, are indicated by else,
e.g. <code>rec = "3=1; 1=2; else=3"</code> (recodes 3 into 1, 1 into 2
and all other values into 3)</p></li>
<li><p><strong>“copy”</strong>: The <code>"else"</code>-token can be
combined with <code>"copy"</code>, indicating that all remaining, not
yet recoded values should stay the same (are copied from the original
value), e.g. <code>rec = "3=1; 1=2; else=copy"</code> (recodes 3 into 1,
1 into 2 and all other values like 2, 4 or 5 etc. will not be recoded,
but copied.</p></li>
<li><p><strong>NA’s</strong>: <code>NA</code> values are allowed both as
old and new value, e.g. <code>rec = "NA=1; 3:5=NA"</code> (recodes all
<code>NA</code> into 1, and all values from 3 to 5 into NA in the new
variable)</p></li>
<li><p><strong>“rev”</strong>: <code>"rev"</code> is a special token
that reverses the value order.</p></li>
<li><p><strong>direct value labelling</strong>: Value labels for new
values can be assigned inside the recode pattern by writing the value
label in square brackets after defining the new value in a recode pair,
e.g. <code>rec = "15:30=1 [young aged]; 31:55=2 [middle aged]; 56:max=3 [old aged]"</code></p></li>
<li><p><strong>non-captured values</strong>: Non-matching values will be
set to <code>NA</code>, unless captured by the <code>"else"</code>- or
<code>"copy"</code>-token.</p></li>
</ul>
<p>Here are some examples:</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="fu">frq</span>(efc<span class="sc">$</span>e42dep)</span>
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a><span class="co">#> elder's dependency (x) <numeric> </span></span>
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a><span class="co">#> # total N=908 valid N=901 mean=2.94 sd=0.94</span></span>
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a><span class="co">#> -------------------------------------------------------------</span></span>
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a><span class="co">#> 1 | independent | 66 | 7.27 | 7.33 | 7.33</span></span>
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="co">#> 2 | slightly dependent | 225 | 24.78 | 24.97 | 32.30</span></span>
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#> 3 | moderately dependent | 306 | 33.70 | 33.96 | 66.26</span></span>
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co">#> 4 | severely dependent | 304 | 33.48 | 33.74 | 100.00</span></span>
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 7 | 0.77 | <NA> | <NA></span></span>
<span id="cb11-12"><a href="#cb11-12" tabindex="-1"></a></span>
<span id="cb11-13"><a href="#cb11-13" tabindex="-1"></a><span class="co"># replace NA with 5</span></span>
<span id="cb11-14"><a href="#cb11-14" tabindex="-1"></a><span class="fu">frq</span>(<span class="fu">rec</span>(efc<span class="sc">$</span>e42dep, <span class="at">rec =</span> <span class="st">"NA=5;else=copy"</span>))</span>
<span id="cb11-15"><a href="#cb11-15" tabindex="-1"></a><span class="co">#> elder's dependency (x) <numeric> </span></span>
<span id="cb11-16"><a href="#cb11-16" tabindex="-1"></a><span class="co">#> # total N=908 valid N=908 mean=2.96 sd=0.95</span></span>
<span id="cb11-17"><a href="#cb11-17" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-18"><a href="#cb11-18" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-19"><a href="#cb11-19" tabindex="-1"></a><span class="co">#> -------------------------------------------------------------</span></span>
<span id="cb11-20"><a href="#cb11-20" tabindex="-1"></a><span class="co">#> 1 | independent | 66 | 7.27 | 7.27 | 7.27</span></span>
<span id="cb11-21"><a href="#cb11-21" tabindex="-1"></a><span class="co">#> 2 | slightly dependent | 225 | 24.78 | 24.78 | 32.05</span></span>
<span id="cb11-22"><a href="#cb11-22" tabindex="-1"></a><span class="co">#> 3 | moderately dependent | 306 | 33.70 | 33.70 | 65.75</span></span>
<span id="cb11-23"><a href="#cb11-23" tabindex="-1"></a><span class="co">#> 4 | severely dependent | 304 | 33.48 | 33.48 | 99.23</span></span>
<span id="cb11-24"><a href="#cb11-24" tabindex="-1"></a><span class="co">#> 5 | 5 | 7 | 0.77 | 0.77 | 100.00</span></span>
<span id="cb11-25"><a href="#cb11-25" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 0 | 0.00 | <NA> | <NA></span></span>
<span id="cb11-26"><a href="#cb11-26" tabindex="-1"></a></span>
<span id="cb11-27"><a href="#cb11-27" tabindex="-1"></a><span class="co"># recode 1 to 2 into 1 and 3 to 4 into 2</span></span>
<span id="cb11-28"><a href="#cb11-28" tabindex="-1"></a><span class="fu">frq</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="cb11-29"><a href="#cb11-29" tabindex="-1"></a><span class="co">#> elder's dependency (x) <numeric> </span></span>
<span id="cb11-30"><a href="#cb11-30" tabindex="-1"></a><span class="co">#> # total N=908 valid N=901 mean=1.68 sd=0.47</span></span>
<span id="cb11-31"><a href="#cb11-31" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-32"><a href="#cb11-32" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-33"><a href="#cb11-33" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb11-34"><a href="#cb11-34" tabindex="-1"></a><span class="co">#> 1 | 291 | 32.05 | 32.30 | 32.30</span></span>
<span id="cb11-35"><a href="#cb11-35" tabindex="-1"></a><span class="co">#> 2 | 610 | 67.18 | 67.70 | 100.00</span></span>
<span id="cb11-36"><a href="#cb11-36" tabindex="-1"></a><span class="co">#> <NA> | 7 | 0.77 | <NA> | <NA></span></span>
<span id="cb11-37"><a href="#cb11-37" tabindex="-1"></a></span>
<span id="cb11-38"><a href="#cb11-38" tabindex="-1"></a><span class="co"># recode 1 to 3 into 4 into 2</span></span>
<span id="cb11-39"><a href="#cb11-39" tabindex="-1"></a><span class="fu">frq</span>(<span class="fu">rec</span>(efc<span class="sc">$</span>e42dep, <span class="at">rec =</span> <span class="st">"min:3=1; 4=2"</span>))</span>
<span id="cb11-40"><a href="#cb11-40" tabindex="-1"></a><span class="co">#> elder's dependency (x) <numeric> </span></span>
<span id="cb11-41"><a href="#cb11-41" tabindex="-1"></a><span class="co">#> # total N=908 valid N=901 mean=1.34 sd=0.47</span></span>
<span id="cb11-42"><a href="#cb11-42" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-43"><a href="#cb11-43" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-44"><a href="#cb11-44" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb11-45"><a href="#cb11-45" tabindex="-1"></a><span class="co">#> 1 | 597 | 65.75 | 66.26 | 66.26</span></span>
<span id="cb11-46"><a href="#cb11-46" tabindex="-1"></a><span class="co">#> 2 | 304 | 33.48 | 33.74 | 100.00</span></span>
<span id="cb11-47"><a href="#cb11-47" tabindex="-1"></a><span class="co">#> <NA> | 7 | 0.77 | <NA> | <NA></span></span>
<span id="cb11-48"><a href="#cb11-48" tabindex="-1"></a></span>
<span id="cb11-49"><a href="#cb11-49" tabindex="-1"></a><span class="co"># recode numeric to character, and remaining values</span></span>
<span id="cb11-50"><a href="#cb11-50" tabindex="-1"></a><span class="co"># into the highest value (="hi") of e42dep</span></span>
<span id="cb11-51"><a href="#cb11-51" tabindex="-1"></a><span class="fu">frq</span>(<span class="fu">rec</span>(efc<span class="sc">$</span>e42dep, <span class="at">rec =</span> <span class="st">"1=first;2=2nd;else=hi"</span>))</span>
<span id="cb11-52"><a href="#cb11-52" tabindex="-1"></a><span class="co">#> elder's dependency (x) <character> </span></span>
<span id="cb11-53"><a href="#cb11-53" tabindex="-1"></a><span class="co">#> # total N=908 valid N=901 mean=2.43 sd=0.86</span></span>
<span id="cb11-54"><a href="#cb11-54" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-55"><a href="#cb11-55" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-56"><a href="#cb11-56" tabindex="-1"></a><span class="co">#> --------------------------------------</span></span>
<span id="cb11-57"><a href="#cb11-57" tabindex="-1"></a><span class="co">#> 2nd | 225 | 24.78 | 24.97 | 24.97</span></span>
<span id="cb11-58"><a href="#cb11-58" tabindex="-1"></a><span class="co">#> first | 66 | 7.27 | 7.33 | 32.30</span></span>
<span id="cb11-59"><a href="#cb11-59" tabindex="-1"></a><span class="co">#> hi | 610 | 67.18 | 67.70 | 100.00</span></span>
<span id="cb11-60"><a href="#cb11-60" tabindex="-1"></a><span class="co">#> <NA> | 7 | 0.77 | <NA> | <NA></span></span>
<span id="cb11-61"><a href="#cb11-61" tabindex="-1"></a></span>
<span id="cb11-62"><a href="#cb11-62" tabindex="-1"></a><span class="fu">data</span>(iris)</span>
<span id="cb11-63"><a href="#cb11-63" tabindex="-1"></a><span class="fu">frq</span>(<span class="fu">rec</span>(iris, Species, <span class="at">rec =</span> <span class="st">"setosa=huhu; else=copy"</span>, <span class="at">append =</span> <span class="cn">FALSE</span>))</span>
<span id="cb11-64"><a href="#cb11-64" tabindex="-1"></a><span class="co">#> Species_r <categorical> </span></span>
<span id="cb11-65"><a href="#cb11-65" tabindex="-1"></a><span class="co">#> # total N=150 valid N=150 mean=2.00 sd=0.82</span></span>
<span id="cb11-66"><a href="#cb11-66" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-67"><a href="#cb11-67" tabindex="-1"></a><span class="co">#> Value | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-68"><a href="#cb11-68" tabindex="-1"></a><span class="co">#> ------------------------------------------</span></span>
<span id="cb11-69"><a href="#cb11-69" tabindex="-1"></a><span class="co">#> huhu | 50 | 33.33 | 33.33 | 33.33</span></span>
<span id="cb11-70"><a href="#cb11-70" tabindex="-1"></a><span class="co">#> versicolor | 50 | 33.33 | 33.33 | 66.67</span></span>
<span id="cb11-71"><a href="#cb11-71" tabindex="-1"></a><span class="co">#> virginica | 50 | 33.33 | 33.33 | 100.00</span></span>
<span id="cb11-72"><a href="#cb11-72" tabindex="-1"></a><span class="co">#> <NA> | 0 | 0.00 | <NA> | <NA></span></span>
<span id="cb11-73"><a href="#cb11-73" tabindex="-1"></a></span>
<span id="cb11-74"><a href="#cb11-74" tabindex="-1"></a><span class="co"># works with mutate</span></span>
<span id="cb11-75"><a href="#cb11-75" tabindex="-1"></a>efc <span class="sc">%>%</span></span>
<span id="cb11-76"><a href="#cb11-76" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">select</span>(e42dep, e17age) <span class="sc">%>%</span></span>
<span id="cb11-77"><a href="#cb11-77" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">mutate</span>(<span class="at">dependency_rev =</span> <span class="fu">rec</span>(e42dep, <span class="at">rec =</span> <span class="st">"rev"</span>)) <span class="sc">%>%</span></span>
<span id="cb11-78"><a href="#cb11-78" tabindex="-1"></a> <span class="fu">head</span>()</span>
<span id="cb11-79"><a href="#cb11-79" tabindex="-1"></a><span class="co">#> e42dep e17age dependency_rev</span></span>
<span id="cb11-80"><a href="#cb11-80" tabindex="-1"></a><span class="co">#> 1 3 83 2</span></span>
<span id="cb11-81"><a href="#cb11-81" tabindex="-1"></a><span class="co">#> 2 3 88 2</span></span>
<span id="cb11-82"><a href="#cb11-82" tabindex="-1"></a><span class="co">#> 3 3 82 2</span></span>
<span id="cb11-83"><a href="#cb11-83" tabindex="-1"></a><span class="co">#> 4 4 67 1</span></span>
<span id="cb11-84"><a href="#cb11-84" tabindex="-1"></a><span class="co">#> 5 4 84 1</span></span>
<span id="cb11-85"><a href="#cb11-85" tabindex="-1"></a><span class="co">#> 6 4 85 1</span></span>
<span id="cb11-86"><a href="#cb11-86" tabindex="-1"></a></span>
<span id="cb11-87"><a href="#cb11-87" tabindex="-1"></a><span class="co"># recode multiple variables and set value labels via recode-syntax</span></span>
<span id="cb11-88"><a href="#cb11-88" tabindex="-1"></a>dummy <span class="ot"><-</span> <span class="fu">rec</span>(</span>
<span id="cb11-89"><a href="#cb11-89" tabindex="-1"></a> efc, c160age, e17age,</span>
<span id="cb11-90"><a href="#cb11-90" tabindex="-1"></a> <span class="at">rec =</span> <span class="st">"15:30=1 [young]; 31:55=2 [middle]; 56:max=3 [old]"</span>,</span>
<span id="cb11-91"><a href="#cb11-91" tabindex="-1"></a> <span class="at">append =</span> <span class="cn">FALSE</span></span>
<span id="cb11-92"><a href="#cb11-92" tabindex="-1"></a>)</span>
<span id="cb11-93"><a href="#cb11-93" tabindex="-1"></a><span class="fu">frq</span>(dummy)</span>
<span id="cb11-94"><a href="#cb11-94" tabindex="-1"></a><span class="co">#> carer' age (c160age_r) <numeric> </span></span>
<span id="cb11-95"><a href="#cb11-95" tabindex="-1"></a><span class="co">#> # total N=908 valid N=901 mean=2.40 sd=0.59</span></span>
<span id="cb11-96"><a href="#cb11-96" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-97"><a href="#cb11-97" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-98"><a href="#cb11-98" tabindex="-1"></a><span class="co">#> -----------------------------------------------</span></span>
<span id="cb11-99"><a href="#cb11-99" tabindex="-1"></a><span class="co">#> 1 | young | 48 | 5.29 | 5.33 | 5.33</span></span>
<span id="cb11-100"><a href="#cb11-100" tabindex="-1"></a><span class="co">#> 2 | middle | 442 | 48.68 | 49.06 | 54.38</span></span>
<span id="cb11-101"><a href="#cb11-101" tabindex="-1"></a><span class="co">#> 3 | old | 411 | 45.26 | 45.62 | 100.00</span></span>
<span id="cb11-102"><a href="#cb11-102" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 7 | 0.77 | <NA> | <NA></span></span>
<span id="cb11-103"><a href="#cb11-103" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-104"><a href="#cb11-104" tabindex="-1"></a><span class="co">#> elder' age (e17age_r) <numeric> </span></span>
<span id="cb11-105"><a href="#cb11-105" tabindex="-1"></a><span class="co">#> # total N=908 valid N=891 mean=3.00 sd=0.00</span></span>
<span id="cb11-106"><a href="#cb11-106" tabindex="-1"></a><span class="co">#> </span></span>
<span id="cb11-107"><a href="#cb11-107" tabindex="-1"></a><span class="co">#> Value | Label | N | Raw % | Valid % | Cum. %</span></span>
<span id="cb11-108"><a href="#cb11-108" tabindex="-1"></a><span class="co">#> -----------------------------------------------</span></span>
<span id="cb11-109"><a href="#cb11-109" tabindex="-1"></a><span class="co">#> 1 | young | 0 | 0.00 | 0 | 0</span></span>
<span id="cb11-110"><a href="#cb11-110" tabindex="-1"></a><span class="co">#> 2 | middle | 0 | 0.00 | 0 | 0</span></span>
<span id="cb11-111"><a href="#cb11-111" tabindex="-1"></a><span class="co">#> 3 | old | 891 | 98.13 | 100 | 100</span></span>
<span id="cb11-112"><a href="#cb11-112" tabindex="-1"></a><span class="co">#> <NA> | <NA> | 17 | 1.87 | <NA> | <NA></span></span></code></pre></div>
</div>
<div id="scoped-variants" class="section level2">
<h2>Scoped variants</h2>
<p>Where applicable, the recoding-functions in <strong>sjmisc</strong>
have “scoped” versions as well, e.g. <code>dicho_if()</code> or
<code>split_var_if()</code>, where transformation will be applied only
to those variables that match the logical condition of
<code>predicate</code>.</p>
</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>
|