Description Usage Arguments Details Value Note See Also Examples
Recode numeric variables into equal sized groups, i.e. a
variable is cut into a smaller number of groups at specific cut points.
split_var_if()
is a scoped variant of split_var()
, where
transformation will be applied only to those variables that match the
logical condition of predicate
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
x |
A vector or data frame. |
... |
Optional, unquoted names of variables that should be selected for
further processing. Required, if |
n |
The new number of groups that |
as.num |
Logical, if |
val.labels |
Optional character vector, to set value label attributes
of recoded variable (see vignette Labelled Data and the sjlabelled-Package).
If |
var.label |
Optional string, to set variable label attribute for the
returned variable (see vignette Labelled Data and the sjlabelled-Package).
If |
inclusive |
Logical; if |
append |
Logical, if |
suffix |
Indicates which suffix will be added to each dummy variable.
Use |
predicate |
A predicate function to be applied to the columns. The
variables for which |
split_var()
splits a variable into equal sized groups, where
the amount of groups depends on the n
-argument. Thus, this
functions cuts
a variable into groups at the specified
quantiles
.
By contrast, group_var
recodes a variable into groups, where
groups have the same value range (e.g., from 1-5, 6-10, 11-15 etc.).
split_var()
also works on grouped data frames
(see group_by
). In this case, splitting is applied to
the subsets of variables in x
. See 'Examples'.
A grouped variable with equal sized groups. If x
is a data frame,
for append = TRUE
, x
including the grouped variables as new
columns is returned; if append = FALSE
, only the grouped variables
will be returned. If append = TRUE
and suffix = ""
,
recoded variables will replace (overwrite) existing variables.
In case a vector has only few number of unique values, splitting into
equal sized groups may fail. In this case, use the inclusive
-argument
to shift a value at the cut point into the lower, preceeding group to
get equal sized groups. See 'Examples'.
group_var
to group variables into equal ranged groups,
or rec
to recode variables.
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 | data(efc)
# non-grouped
table(efc$neg_c_7)
# split into 3 groups
table(split_var(efc$neg_c_7, n = 3))
# split multiple variables into 3 groups
split_var(efc, neg_c_7, pos_v_4, e17age, n = 3, append = FALSE)
frq(split_var(efc, neg_c_7, pos_v_4, e17age, n = 3, append = FALSE))
# original
table(efc$e42dep)
# two groups, non-inclusive cut-point
# vector split leads to unequal group sizes
table(split_var(efc$e42dep, n = 2))
# two groups, inclusive cut-point
# group sizes are equal
table(split_var(efc$e42dep, n = 2, inclusive = TRUE))
# Unlike dplyr's ntile(), split_var() never splits a value
# into two different categories, i.e. you always get a clean
# separation of original categories
library(dplyr)
x <- dplyr::ntile(efc$neg_c_7, n = 3)
table(efc$neg_c_7, x)
x <- split_var(efc$neg_c_7, n = 3)
table(efc$neg_c_7, x)
# works also with gouped data frames
mtcars %>%
split_var(disp, n = 3, append = FALSE) %>%
table()
mtcars %>%
group_by(cyl) %>%
split_var(disp, n = 3, append = FALSE) %>%
table()
|
sh: 1: cannot create /dev/null: Permission denied
sh: 1: wc: Permission denied
Could not detect number of cores, defaulting to 1.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 27
75 99 106 120 96 85 64 54 45 30 35 26 16 16 2 7 4 3 6 1
28
2
1 2 3
280 301 311
neg_c_7_g pos_v_4_g e17age_g
1 2 2 3
2 3 1 3
3 2 2 2
4 2 3 1
5 2 3 3
6 3 1 3
7 3 2 1
8 2 3 3
9 3 2 2
10 2 2 3
11 3 1 1
12 3 1 3
13 3 3 2
14 3 3 2
15 3 1 2
16 3 3 3
17 2 3 2
18 2 2 1
19 3 2 3
20 3 2 2
21 2 1 3
22 1 2 1
23 1 3 3
24 3 3 3
25 2 3 3
26 3 2 2
27 <NA> 1 3
28 2 1 2
29 3 1 3
30 2 <NA> 3
31 3 2 3
32 1 2 3
33 3 1 3
34 3 2 2
35 1 3 2
36 2 3 2
37 3 1 1
38 1 3 3
39 3 3 3
40 3 2 3
41 3 2 3
42 3 3 1
43 3 1 1
44 3 1 1
45 3 2 1
46 <NA> 1 2
47 3 1 1
48 3 2 1
49 3 3 2
50 3 1 3
51 3 3 1
52 2 <NA> 2
53 3 1 1
54 3 1 1
55 1 3 2
56 2 3 3
57 3 <NA> <NA>
58 <NA> 1 2
59 3 1 2
60 3 1 1
61 3 3 3
62 3 3 3
63 1 3 3
64 3 1 1
65 2 <NA> 3
66 3 3 3
67 3 1 2
68 2 2 2
69 3 <NA> 2
70 3 1 1
71 1 3 2
72 3 2 <NA>
73 2 2 3
74 3 1 1
75 2 3 3
76 2 2 2
77 3 1 1
78 2 2 3
79 2 1 2
80 3 1 2
81 1 3 2
82 3 2 <NA>
83 2 3 2
84 3 1 2
85 2 3 2
86 3 3 2
87 3 1 1
88 3 2 1
89 3 1 2
90 2 <NA> 3
91 2 2 2
92 2 3 2
93 3 2 1
94 2 3 3
95 2 2 1
96 3 <NA> 3
97 2 <NA> 1
98 2 1 3
99 2 3 3
100 3 1 2
101 3 1 2
102 2 1 1
103 2 3 2
104 1 1 2
105 3 2 3
106 3 2 1
107 2 3 2
108 2 2 2
109 3 2 2
110 3 2 2
111 2 3 1
112 2 1 2
113 3 1 3
114 1 3 2
115 3 2 1
116 3 2 3
117 2 2 3
118 3 2 2
119 3 1 1
120 3 1 3
121 1 3 3
122 3 <NA> 1
123 3 <NA> 3
124 3 1 1
125 3 2 3
126 3 2 1
127 1 <NA> 2
128 3 <NA> 3
129 1 3 3
130 3 3 1
131 3 1 3
132 1 3 2
133 3 2 2
134 <NA> <NA> 2
135 3 1 3
136 2 2 3
137 3 2 3
138 1 3 2
139 2 3 2
140 2 3 3
141 3 1 2
142 2 1 3
143 3 1 3
144 <NA> <NA> 3
145 2 2 3
146 2 3 3
147 <NA> 2 1
148 2 1 1
149 2 2 2
150 3 2 3
151 2 3 3
152 3 2 3
153 2 2 3
154 2 3 3
155 2 3 1
156 2 2 1
157 1 2 2
158 3 3 3
159 2 2 2
160 2 2 1
161 3 1 2
162 3 2 3
163 3 1 3
164 2 1 <NA>
165 2 3 2
166 3 3 1
167 <NA> <NA> 3
168 3 <NA> 2
169 2 1 2
170 2 <NA> 2
171 1 3 1
172 2 3 3
173 1 3 2
174 2 3 1
175 2 3 3
176 2 2 1
177 2 1 3
178 3 2 3
179 1 2 3
180 2 3 2
181 2 3 3
182 2 2 3
183 3 1 2
184 2 2 2
185 3 <NA> 2
186 3 3 2
187 <NA> 3 1
188 3 1 3
189 1 2 2
190 2 3 1
191 3 2 2
192 3 3 <NA>
193 2 1 3
194 2 2 1
195 1 3 3
196 1 3 2
197 3 <NA> 3
198 3 <NA> 3
199 3 1 3
200 1 2 3
201 1 3 3
202 3 1 3
203 1 3 3
204 3 1 3
205 1 3 3
206 1 2 2
207 3 2 3
208 2 3 3
209 2 3 3
210 2 2 3
211 2 2 3
212 3 1 3
213 1 2 2
214 3 1 3
215 3 3 1
216 1 3 3
217 3 3 3
218 3 1 2
219 2 3 2
220 1 3 1
221 2 1 3
222 3 2 3
223 2 2 3
224 2 2 3
225 3 2 3
226 1 1 2
227 3 3 3
228 3 1 3
229 2 3 1
230 3 2 1
231 3 3 3
232 3 2 3
233 2 3 3
234 2 1 3
235 2 2 3
236 2 1 3
237 2 3 1
238 2 2 2
239 1 3 2
240 2 3 2
241 1 2 2
242 2 2 2
243 2 1 3
244 2 2 3
245 1 2 3
246 2 3 2
247 1 2 2
248 1 1 3
249 2 3 3
250 2 1 3
251 2 1 2
252 2 1 2
253 3 3 1
254 2 1 1
255 2 1 3
256 3 1 3
257 2 3 1
258 3 1 3
259 2 2 3
260 3 2 3
261 1 2 2
262 3 3 3
263 2 1 3
264 2 2 2
265 1 3 3
266 2 2 2
267 3 1 1
268 <NA> 1 3
269 2 2 3
270 3 1 2
271 1 2 2
272 2 1 1
273 1 1 2
274 2 2 1
275 3 1 1
276 2 2 2
277 1 2 2
278 1 2 1
279 1 3 1
280 1 3 1
281 1 3 1
282 1 3 1
283 1 3 1
284 2 3 1
285 1 3 2
286 2 3 1
287 2 1 3
288 1 3 3
289 3 1 3
290 3 1 1
291 2 1 3
292 1 3 3
293 1 1 2
294 1 3 2
295 1 2 3
296 1 2 1
297 3 2 1
298 1 2 2
299 2 2 1
300 1 3 1
301 3 2 3
302 1 1 1
303 3 1 2
304 3 3 3
305 1 3 2
306 3 3 3
307 3 2 3
308 1 2 2
309 1 1 1
310 1 3 1
311 3 2 3
312 3 1 1
313 3 1 1
314 1 3 3
315 2 2 1
316 3 2 1
317 2 1 1
318 3 1 2
319 2 1 3
320 1 3 3
321 2 3 3
322 1 3 3
323 1 3 3
324 2 2 3
325 1 2 3
326 2 2 3
327 3 2 3
328 3 1 2
329 1 3 2
330 2 1 <NA>
331 2 2 2
332 2 2 2
333 3 3 3
334 2 3 3
335 1 3 3
336 1 3 1
337 3 1 3
338 1 3 3
339 3 2 3
340 1 3 2
341 1 3 1
342 3 3 1
343 1 1 2
344 1 3 <NA>
345 1 2 2
346 1 2 3
347 1 2 1
348 3 1 3
349 1 3 2
350 1 3 3
351 1 1 2
352 1 3 3
353 2 1 2
354 2 2 2
355 2 1 1
356 3 1 2
357 1 2 3
358 2 2 1
359 3 1 2
360 1 2 3
361 2 2 3
362 1 1 2
363 1 3 2
364 2 3 1
365 1 2 1
366 1 3 1
367 1 3 1
368 2 2 2
369 2 1 2
370 1 3 2
371 1 3 3
372 2 2 3
373 3 2 3
374 3 1 2
375 2 2 3
376 3 1 2
377 2 2 1
378 1 3 2
379 1 2 1
380 2 1 2
381 3 1 1
382 2 1 1
383 3 2 2
384 3 1 3
385 1 3 2
386 1 2 1
387 1 3 2
388 3 3 3
389 2 3 3
390 3 1 2
391 3 1 3
392 2 2 3
393 1 2 1
394 3 2 1
395 3 2 2
396 3 2 1
397 3 1 2
398 3 1 2
399 3 2 1
400 1 3 2
401 2 2 2
402 2 2 1
403 3 1 3
404 2 3 3
405 3 2 1
406 2 1 2
407 1 2 2
408 1 2 3
409 2 1 3
410 3 2 3
411 3 3 3
412 1 1 1
413 2 2 1
414 2 2 1
415 1 2 1
416 2 2 1
417 3 1 1
418 2 2 1
419 3 1 1
420 2 2 3
421 3 2 3
422 2 2 2
423 3 3 2
424 2 2 3
425 2 1 3
426 3 1 1
427 2 2 2
428 2 3 2
429 2 3 3
430 2 3 3
431 1 1 1
432 3 1 2
433 2 2 3
434 2 2 2
435 3 1 3
436 3 2 2
437 3 1 2
438 2 2 2
439 1 3 1
440 3 1 3
441 2 1 2
442 3 3 2
443 3 3 1
444 2 2 3
445 3 1 3
446 2 2 2
447 2 2 3
448 2 1 <NA>
449 3 1 3
450 3 2 1
451 1 3 1
452 2 2 2
453 1 3 2
454 1 3 2
455 1 2 3
456 3 1 2
457 1 3 2
458 1 2 3
459 1 3 3
460 2 3 2
461 1 2 3
462 2 3 2
463 2 2 3
464 3 1 3
465 3 2 2
466 3 2 3
467 3 2 2
468 1 3 2
469 1 2 1
470 1 2 2
471 3 2 3
472 1 3 1
473 2 2 2
474 1 3 3
475 1 3 1
476 1 3 2
477 2 1 1
478 3 2 1
479 1 3 1
480 2 2 2
481 3 3 1
482 1 1 2
483 1 3 2
484 1 3 2
485 2 2 2
486 2 3 2
487 3 3 3
488 1 1 3
489 1 3 1
490 1 2 3
491 2 2 2
492 1 2 1
493 2 2 3
494 2 1 2
495 3 1 <NA>
496 2 2 2
497 1 2 3
498 2 1 2
499 3 1 1
500 2 2 2
501 3 2 1
502 1 2 2
503 1 2 1
504 3 2 3
505 2 1 2
506 3 1 3
507 3 1 1
508 3 1 1
509 3 1 1
510 3 1 3
511 2 2 2
512 3 1 2
513 3 3 1
514 1 3 3
515 1 3 3
516 3 1 1
517 1 3 3
518 3 <NA> 2
519 3 2 1
520 2 2 2
521 1 2 1
522 3 3 3
523 1 2 1
524 2 2 2
525 1 3 2
526 2 1 2
527 1 1 3
528 1 3 2
529 2 1 2
530 1 3 3
531 3 1 3
532 1 3 1
533 2 2 2
534 1 2 3
535 1 2 2
536 1 2 1
537 2 2 3
538 1 1 3
539 3 1 2
540 2 2 3
541 <NA> 2 3
542 1 3 3
543 1 3 2
544 1 2 1
545 1 3 1
546 1 3 1
547 1 3 1
548 3 2 2
549 3 1 2
550 1 1 2
551 1 3 1
552 1 3 2
553 2 2 3
554 1 3 3
555 1 2 1
556 1 3 1
557 3 3 2
558 3 3 3
559 3 3 2
560 3 3 1
561 3 3 2
562 2 3 3
563 3 2 1
564 1 1 3
565 1 3 1
566 1 3 3
567 1 3 3
568 1 3 1
569 3 2 <NA>
570 3 1 3
571 3 1 1
572 3 2 2
573 3 1 3
574 3 1 2
575 3 1 3
576 2 2 2
577 2 1 3
578 3 1 1
579 1 3 2
580 3 1 1
581 2 3 2
582 3 1 3
583 3 1 3
584 3 1 3
585 2 2 1
586 3 1 2
587 2 2 3
588 2 2 2
589 2 2 2
590 1 3 1
591 1 2 3
592 3 1 3
593 3 1 3
594 2 3 1
595 1 3 1
596 3 1 3
597 3 1 1
598 2 1 1
599 3 1 3
600 3 2 3
601 3 2 3
602 2 2 3
603 3 1 2
604 1 3 2
605 1 3 2
606 2 1 3
607 3 2 1
608 3 1 1
609 3 2 2
610 1 3 2
611 2 3 1
612 2 2 1
613 3 1 1
614 2 3 1
615 3 2 3
616 1 1 <NA>
617 1 3 3
618 1 3 1
619 1 2 2
620 3 3 3
621 2 1 2
622 1 2 2
623 1 1 3
624 2 3 1
625 3 1 3
626 1 3 1
627 2 3 1
628 2 3 3
629 2 3 3
630 2 3 3
631 2 2 2
632 2 1 1
633 1 1 1
634 3 1 2
635 3 1 3
636 1 1 1
637 1 2 1
638 1 2 2
639 1 3 3
640 3 1 1
641 3 1 3
642 3 3 1
643 1 3 2
644 2 3 2
645 1 3 1
646 2 2 1
647 2 2 2
648 2 2 2
649 2 2 1
650 2 2 2
651 2 3 1
652 2 3 3
653 2 1 1
654 3 1 3
655 1 3 1
656 1 3 2
657 2 3 1
658 1 3 2
659 1 3 2
660 3 3 2
661 2 3 2
662 2 2 1
663 3 1 2
664 1 2 1
665 2 2 2
666 3 2 1
667 2 2 1
668 1 1 3
669 2 2 1
670 2 2 1
671 2 1 1
672 1 1 2
673 2 1 1
674 1 3 1
675 3 1 2
676 2 2 1
677 1 3 3
678 2 2 3
679 1 2 1
680 1 3 3
681 1 3 1
682 1 3 1
683 1 3 2
684 1 3 3
685 1 2 3
686 2 2 1
687 1 2 1
688 2 2 1
689 2 2 1
690 2 2 2
691 1 3 2
692 3 2 2
693 2 2 2
694 2 2 1
695 1 3 3
696 2 2 3
697 3 1 1
698 2 1 3
699 2 2 3
700 3 1 3
701 3 1 1
702 2 2 1
703 3 2 1
704 1 2 3
705 2 2 2
706 3 2 3
707 1 1 1
708 1 2 2
709 2 1 1
710 1 2 1
711 1 2 3
712 3 2 1
713 2 1 1
714 3 2 1
715 2 2 1
716 3 2 2
717 3 2 2
718 1 1 1
719 1 3 1
720 3 1 3
721 1 3 1
722 2 3 1
723 1 2 3
724 3 1 2
725 3 1 2
726 3 1 3
727 3 1 2
728 2 1 2
729 3 1 3
730 2 2 2
731 2 2 2
732 3 1 2
733 2 3 2
734 2 3 3
735 2 3 1
736 2 2 3
737 2 1 1
738 1 3 2
739 3 3 3
740 1 3 3
741 3 3 3
742 1 2 2
743 3 1 3
744 2 2 2
745 2 1 3
746 2 2 2
747 1 1 2
748 2 1 2
749 3 3 3
750 3 2 3
751 1 3 2
752 1 3 3
753 3 2 3
754 2 2 3
755 2 1 2
756 2 2 1
757 1 3 2
758 1 3 3
759 3 1 3
760 1 3 1
761 1 3 1
762 3 1 3
763 1 3 2
764 3 1 3
765 1 3 2
766 1 3 3
767 3 1 1
768 1 3 1
769 1 3 1
770 1 3 2
771 1 3 3
772 1 2 2
773 1 2 3
774 1 2 3
775 1 3 3
776 3 1 1
777 2 1 3
778 2 1 1
779 3 1 1
780 2 2 2
781 1 1 2
782 2 1 1
783 1 3 1
784 1 1 1
785 3 1 2
786 2 2 2
787 3 1 1
788 2 2 1
789 2 2 3
790 1 3 1
791 2 3 1
792 2 1 1
793 1 1 1
794 3 1 3
795 1 3 3
796 2 1 2
797 3 2 3
798 2 2 1
799 2 1 2
800 3 3 3
801 1 3 3
802 3 3 2
803 3 3 2
804 3 2 1
805 1 3 2
806 2 3 2
807 1 3 2
808 1 3 3
809 1 3 3
810 1 3 3
811 1 2 2
812 1 2 2
813 1 2 1
814 3 1 2
815 1 2 1
816 2 2 2
817 3 1 2
818 2 1 3
819 1 3 1
820 2 1 1
821 1 2 1
822 3 2 1
823 3 2 1
824 3 1 1
825 3 3 1
826 3 3 1
827 3 3 1
828 2 1 2
829 3 1 1
830 2 2 1
831 2 3 3
832 2 3 2
833 1 3 2
834 2 1 3
835 2 1 1
836 2 1 1
837 2 1 1
838 1 2 3
839 1 3 1
840 3 3 1
841 3 3 3
842 2 3 3
843 1 3 2
844 2 3 2
845 1 2 1
846 1 1 1
847 1 3 3
848 1 1 2
849 2 2 2
850 1 3 2
851 1 2 1
852 2 3 1
853 2 2 3
854 1 3 2
855 2 3 3
856 1 3 3
857 2 3 3
858 2 1 1
859 3 1 3
860 1 2 1
861 1 3 2
862 2 3 1
863 2 2 2
864 2 1 3
865 2 2 3
866 1 3 2
867 1 2 2
868 1 3 1
869 2 2 1
870 1 3 2
871 1 3 1
872 1 2 2
873 1 1 1
874 1 1 2
875 1 3 2
876 3 1 1
877 3 1 3
878 2 1 2
879 3 1 1
880 1 1 2
881 1 1 3
882 1 1 1
883 2 1 2
884 3 1 3
885 3 1 1
886 3 2 2
887 3 1 2
888 1 3 1
889 2 2 1
890 3 1 3
891 1 3 1
892 1 2 2
893 3 3 2
894 3 1 3
895 2 1 3
896 3 1 3
897 3 1 1
898 1 3 2
899 1 2 2
900 3 3 1
901 2 3 1
902 2 2 1
903 <NA> <NA> <NA>
904 <NA> <NA> <NA>
905 <NA> <NA> <NA>
906 <NA> <NA> <NA>
907 <NA> <NA> <NA>
908 <NA> <NA> <NA>
# Negative impact with 7 items (neg_c_7_g) <categorical>
# total N=908 valid N=892 mean=2.03 sd=0.81
val frq raw.prc valid.prc cum.prc
1 280 30.84 31.39 31.39
2 301 33.15 33.74 65.13
3 311 34.25 34.87 100.00
<NA> 16 1.76 NA NA
# Positive value with 4 items (pos_v_4_g) <categorical>
# total N=908 valid N=881 mean=2.03 sd=0.81
val frq raw.prc valid.prc cum.prc
1 275 30.29 31.21 31.21
2 304 33.48 34.51 65.72
3 302 33.26 34.28 100.00
<NA> 27 2.97 NA NA
# elder' age (e17age_g) <categorical>
# total N=908 valid N=891 mean=2.05 sd=0.82
val frq raw.prc valid.prc cum.prc
1 274 30.18 30.75 30.75
2 294 32.38 33.00 63.75
3 323 35.57 36.25 100.00
<NA> 17 1.87 NA NA
1 2 3 4
66 225 306 304
1 2
291 610
1 2
597 304
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
x
1 2 3
7 75 0 0
8 99 0 0
9 106 0 0
10 18 102 0
11 0 96 0
12 0 85 0
13 0 14 50
14 0 0 54
15 0 0 45
16 0 0 30
17 0 0 35
18 0 0 26
19 0 0 16
20 0 0 16
21 0 0 2
22 0 0 7
23 0 0 4
24 0 0 3
25 0 0 6
27 0 0 1
28 0 0 2
x
1 2 3
7 75 0 0
8 99 0 0
9 106 0 0
10 0 120 0
11 0 96 0
12 0 85 0
13 0 0 64
14 0 0 54
15 0 0 45
16 0 0 30
17 0 0 35
18 0 0 26
19 0 0 16
20 0 0 16
21 0 0 2
22 0 0 7
23 0 0 4
24 0 0 3
25 0 0 6
27 0 0 1
28 0 0 2
.
1 2 3
11 10 11
.
1 2 3
10 8 14
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.