Convenient function to create a recode pattern for the
rec
function, which recodes (numeric)
vectors into smaller groups.
rec_pattern(from, to, width = 5, other = NULL)
from | Minimum value that should be recoded. |
---|---|
to | Maximum value that should be recoded. |
width | Numeric, indicating the range of each group. |
other | String token, indicating how to deal with all other values
that have not been captured by the recode pattern. See 'Details'
on the |
A list with two values:
pattern
string pattern that can be used as rec
argument for the rec
-function.
labels
the associated values labels that can be used with set_labels
.
group_var
for recoding variables into smaller groups, and
group_labels
to create the asssociated value labels.
rp <- rec_pattern(1, 100) rp#> $pattern #> [1] "1:5=1;6:10=2;11:15=3;16:20=4;21:25=5;26:30=6;31:35=7;36:40=8;41:45=9;46:50=10;51:55=11;56:60=12;61:65=13;66:70=14;71:75=15;76:80=16;81:85=17;86:90=18;91:95=19;96:100=20;" #> #> $labels #> 1 2 3 4 5 6 7 8 #> "1-5" "6-10" "11-15" "16-20" "21-25" "26-30" "31-35" "36-40" #> 9 10 11 12 13 14 15 16 #> "41-45" "46-50" "51-55" "56-60" "61-65" "66-70" "71-75" "76-80" #> 17 18 19 20 #> "81-85" "86-90" "91-95" "96-100" #># sample data, inspect age of carers data(efc) table(efc$c160age, exclude = NULL)#> #> 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 #> 1 2 2 1 2 4 3 5 3 4 4 8 9 9 5 10 #> 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 #> 9 17 6 7 13 12 13 15 21 24 25 14 22 27 26 32 #> 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 #> 19 15 22 27 28 24 27 21 26 20 28 19 28 25 25 26 #> 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 #> 18 17 14 18 15 4 11 7 7 12 10 7 5 5 5 3 #> 82 83 85 87 89 <NA> #> 1 4 1 1 1 7#> #> 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <NA> #> 5 15 28 50 51 99 126 116 122 123 82 41 32 9 2 7# recode carers age into groups of width 5 x <- rec( efc$c160age, rec = rp$pattern, val.labels = rp$labels ) # watch result frq(x)#> #> # carer' age (x) <numeric> #> # total N=908 valid N=901 mean=11.07 sd=2.69 #> #> val label frq raw.prc valid.prc cum.prc #> 1 1-5 0 0.00 0.00 0.00 #> 2 6-10 0 0.00 0.00 0.00 #> 3 11-15 0 0.00 0.00 0.00 #> 4 16-20 5 0.55 0.55 0.55 #> 5 21-25 15 1.65 1.66 2.22 #> 6 26-30 28 3.08 3.11 5.33 #> 7 31-35 50 5.51 5.55 10.88 #> 8 36-40 51 5.62 5.66 16.54 #> 9 41-45 99 10.90 10.99 27.52 #> 10 46-50 126 13.88 13.98 41.51 #> 11 51-55 116 12.78 12.87 54.38 #> 12 56-60 122 13.44 13.54 67.92 #> 13 61-65 123 13.55 13.65 81.58 #> 14 66-70 82 9.03 9.10 90.68 #> 15 71-75 41 4.52 4.55 95.23 #> 16 76-80 32 3.52 3.55 98.78 #> 17 81-85 9 0.99 1.00 99.78 #> 18 86-90 2 0.22 0.22 100.00 #> 19 91-95 0 0.00 0.00 100.00 #> 20 96-100 0 0.00 0.00 100.00 #> NA NA 7 0.77 NA NA #> #>