Recode numeric variables into equal ranged, grouped factors, i.e. a variable is cut into a smaller number of groups, where each group has the same value range. group_labels() creates the related value labels. group_var_if() and group_labels_if() are scoped variants of group_var() and group_labels(), where grouping will be applied only to those variables that match the logical condition of predicate.

group_var(
  x,
  ...,
  size = 5,
  as.num = TRUE,
  right.interval = FALSE,
  n = 30,
  append = TRUE,
  suffix = "_gr"
)

group_var_if(
  x,
  predicate,
  size = 5,
  as.num = TRUE,
  right.interval = FALSE,
  n = 30,
  append = TRUE,
  suffix = "_gr"
)

group_labels(x, ..., size = 5, right.interval = FALSE, n = 30)

group_labels_if(x, predicate, size = 5, right.interval = FALSE, n = 30)

Arguments

x

A vector or data frame.

...

Optional, unquoted names of variables that should be selected for further processing. Required, if x is a data frame (and no vector) and only selected variables from x should be processed. You may also use functions like : or tidyselect's select-helpers. See 'Examples' or package-vignette.

size

Numeric; group-size, i.e. the range for grouping. By default, for each 5 categories of x a new group is defined, i.e. size = 5. Use size = "auto" to automatically resize a variable into a maximum of 30 groups (which is the ggplot-default grouping when plotting histograms). Use n to determine the amount of groups.

as.num

Logical, if TRUE, return value will be numeric, not a factor.

right.interval

Logical; if TRUE, grouping starts with the lower bound of size. See 'Details'.

n

Sets the maximum number of groups that are defined when auto-grouping is on (size = "auto"). Default is 30. If size is not set to "auto", this argument will be ignored.

append

Logical, if TRUE (the default) and x is a data frame, x including the new variables as additional columns is returned; if FALSE, only the new variables are returned.

suffix

Indicates which suffix will be added to each dummy variable. Use "numeric" to number dummy variables, e.g. x_1, x_2, x_3 etc. Use "label" to add value label, e.g. x_low, x_mid, x_high. May be abbreviated.

predicate

A predicate function to be applied to the columns. The variables for which predicate returns TRUE are selected.

Value

  • For group_var(), a grouped variable, either as numeric or as factor (see paramter as.num). If x is a data frame, only the grouped variables will be returned.

  • For group_labels(), a string vector or a list of string vectors containing labels based on the grouped categories of x, formatted as "from lower bound to upper bound", e.g. "10-19" "20-29" "30-39" etc. See 'Examples'.

Details

If size is set to a specific value, the variable is recoded into several groups, where each group has a maximum range of size. Hence, the amount of groups differ depending on the range of x.

If size = "auto", the variable is recoded into a maximum of n groups. Hence, independent from the range of x, always the same amount of groups are created, so the range within each group differs (depending on x's range).

right.interval determins which boundary values to include when grouping is done. If TRUE, grouping starts with the lower bound of size. For example, having a variable ranging from 50 to 80, groups cover the ranges from 50-54, 55-59, 60-64 etc. If FALSE (default), grouping starts with the upper bound of size. In this case, groups cover the ranges from 46-50, 51-55, 56-60, 61-65 etc. Note: This will cover a range from 46-50 as first group, even if values from 46 to 49 are not present. See 'Examples'.

If you want to split a variable into a certain amount of equal sized groups (instead of having groups where values have all the same range), use the split_var function!

group_var() also works on grouped data frames (see group_by). In this case, grouping is applied to the subsets of variables in x. See 'Examples'.

Note

Variable label attributes (see, for instance, set_label) are preserved. Usually you should use the same values for size and right.interval in group_labels() as used in the group_var function if you want matching labels for the related recoded variable.

See also

split_var to split variables into equal sized groups, group_str for grouping string vectors or rec_pattern and rec for another convenient way of recoding variables into smaller groups.

Examples

age <- abs(round(rnorm(100, 65, 20)))
age.grp <- group_var(age, size = 10)
hist(age)

hist(age.grp)


age.grpvar <- group_labels(age, size = 10)
table(age.grp)
#> age.grp
#>  1  2  3  4  5  6  7  8  9 
#>  1  4 10 21 22 19 10  9  4 
print(age.grpvar)
#> [1] "10-19"   "30-39"   "40-49"   "50-59"   "60-69"   "70-79"   "80-89"  
#> [8] "90-99"   "100-109"

# histogram with EUROFAMCARE sample dataset
# variable not grouped
library(sjlabelled)
data(efc)
hist(efc$e17age, main = get_label(efc$e17age))


# bar plot with EUROFAMCARE sample dataset
# grouped variable
ageGrp <- group_var(efc$e17age)
ageGrpLab <- group_labels(efc$e17age)
barplot(table(ageGrp), main = get_label(efc$e17age), names.arg = ageGrpLab)


# within a pipe-chain
library(dplyr)
efc %>%
  select(e17age, c12hour, c160age) %>%
  group_var(size = 20)
#>     e17age c12hour c160age e17age_gr c12hour_gr c160age_gr
#> 1       83      16      56         2          1          3
#> 2       88     148      54         2          8          3
#> 3       82      70      80         2          4          5
#> 4       67     168      69         1          9          4
#> 5       84     168      47         2          9          3
#> 6       85      16      56         2          1          3
#> 7       74     161      61         1          9          4
#> 8       87     110      67         2          6          4
#> 9       79      28      59         1          2          3
#> 10      83      40      49         2          3          3
#> 11      68     100      66         1          6          4
#> 12      97      25      47         2          2          3
#> 13      80      25      58         2          2          3
#> 14      75      24      75         1          2          4
#> 15      82      56      49         2          3          3
#> 16      89      20      56         2          2          3
#> 17      80      25      75         2          2          4
#> 18      72     126      70         1          7          4
#> 19      94     168      52         2          9          3
#> 20      79     118      48         1          6          3
#> 21      89     150      58         2          8          3
#> 22      67      50      65         1          3          4
#> 23      94      18      49         2          1          3
#> 24      83     168      60         2          9          4
#> 25      85      15      55         2          1          3
#> 26      80     168      62         2          9          4
#> 27      88      12      68         2          1          4
#> 28      76       7      76         1          1          4
#> 29      84      35      58         2          2          3
#> 30      95     168      65         2          9          4
#> 31      88     150      63         2          8          4
#> 32      87     168      79         2          9          4
#> 33      89     168      65         2          9          4
#> 34      80     119      74         2          6          4
#> 35      75     168      76         1          9          4
#> 36      82     168      73         2          9          4
#> 37      69     168      67         1          9          4
#> 38      91      28      62         2          2          4
#> 39      86     168      80         2          9          5
#> 40      86      30      49         2          2          3
#> 41      84      14      46         2          1          3
#> 42      69     168      68         1          9          4
#> 43      67     168      62         1          9          4
#> 44      67      50      65         1          3          4
#> 45      66     168      63         1          9          4
#> 46      79      24      81         1          2          5
#> 47      72     168      72         1          9          4
#> 48      65      42      64         1          3          4
#> 49      75     154      73         1          8          4
#> 50      87      60      73         2          4          4
#> 51      68     168      64         1          9          4
#> 52      75     168      67         1          9          4
#> 53      65      24      60         1          2          4
#> 54      67     168      64         1          9          4
#> 55      81     150      85         2          8          5
#> 56      83     168      55         2          9          3
#> 57      NA     168      72        NA          9          4
#> 58      82     168      52         2          9          3
#> 59      79     168      63         1          9          4
#> 60      72     168      69         1          9          4
#> 61      87     128      61         2          7          4
#> 62      85     168      79         2          9          4
#> 63      88      11      64         2          1          4
#> 64      74      50      44         1          3          3
#> 65      87      80      59         2          5          3
#> 66      88      15      52         2          1          3
#> 67      76       7      46         1          1          3
#> 68      81      21      59         2          2          3
#> 69      80     168      83         2          9          5
#> 70      68      24      76         1          2          4
#> 71      82       6      31         2          1          2
#> 72      NA      30      60        NA          2          4
#> 73      91     168      57         2          9          3
#> 74      66      42      63         1          3          4
#> 75      89      30      59         2          2          3
#> 76      79      85      72         1          5          4
#> 77      69      35      47         1          2          3
#> 78      92      70      56         2          4          3
#> 79      75       9      47         1          1          3
#> 80      76     168      69         1          9          4
#> 81      77      77      74         1          4          4
#> 82      NA      24      67        NA          2          4
#> 83      76      91      72         1          5          4
#> 84      76       6      48         1          1          3
#> 85      79      22      55         1          2          3
#> 86      77     168      57         1          9          3
#> 87      67     168      62         1          9          4
#> 88      66     168      70         1          9          4
#> 89      78     168      34         1          9          2
#> 90      84      50      55         2          3          3
#> 91      79      40      69         1          3          4
#> 92      77       9      69         1          1          4
#> 93      66      25      60         1          2          4
#> 94      94      14      66         2          1          4
#> 95      65      49      60         1          3          4
#> 96      90       5      54         2          1          3
#> 97      66       7      64         1          1          4
#> 98      90      21      75         2          2          4
#> 99      85      24      80         2          2          5
#> 100     77       7      49         1          1          3
#> 101     76     168      68         1          9          4
#> 102     66     168      64         1          9          4
#> 103     76       4      72         1          1          4
#> 104     80     168      54         2          9          3
#> 105     89     168      62         2          9          4
#> 106     69      15      65         1          1          4
#> 107     75     168      75         1          9          4
#> 108     75     168      77         1          9          4
#> 109     80      20      50         2          2          3
#> 110     80      59      54         2          3          3
#> 111     74      24      72         1          2          4
#> 112     82       7      46         2          1          3
#> 113     91     100      61         2          6          4
#> 114     80     168      83         2          9          5
#> 115     74     168      44         1          9          3
#> 116     90      28      67         2          2          4
#> 117     83      20      61         2          2          4
#> 118     82      50      78         2          3          4
#> 119     73      89      77         1          5          4
#> 120     84     168      75         2          9          4
#> 121     90      12      61         2          1          4
#> 122     65      24      65         1          2          4
#> 123     84      91      74         2          5          4
#> 124     72     168      63         1          9          4
#> 125     95     168      66         2          9          4
#> 126     67     168      62         1          9          4
#> 127     81      11      49         2          1          3
#> 128     92      35      55         2          2          3
#> 129     83      10      45         2          1          3
#> 130     74     125      76         1          7          4
#> 131     91     140      65         2          8          4
#> 132     78      28      56         1          2          3
#> 133     81      84      75         2          5          4
#> 134     76      15      45         1          1          3
#> 135     93      15      59         2          1          3
#> 136     97      14      66         2          1          4
#> 137     93      24      61         2          2          4
#> 138     81       4      50         2          1          3
#> 139     75     140      75         1          8          4
#> 140     84     168      76         2          9          4
#> 141     78       6      35         1          1          2
#> 142     84      10      42         2          1          3
#> 143     87      20      65         2          2          4
#> 144     83      15      81         2          1          5
#> 145     94      65      43         2          4          3
#> 146     85      35      76         2          2          4
#> 147     70     105      67         1          6          4
#> 148     69      50      63         1          3          4
#> 149     76     168      56         1          9          3
#> 150     83      77      49         2          4          3
#> 151     91     168      55         2          9          3
#> 152     90      42      60         2          3          4
#> 153     87       4      59         2          1          3
#> 154     91     168      62         2          9          4
#> 155     72     168      62         1          9          4
#> 156     73      42      79         1          3          4
#> 157     79       6      54         1          1          3
#> 158     84     168      57         2          9          3
#> 159     82      70      74         2          4          4
#> 160     65      15      67         1          1          4
#> 161     78      30      51         1          2          3
#> 162     94       9      55         2          1          3
#> 163     91      16      56         2          1          3
#> 164     NA      35      58        NA          2          3
#> 165     80      28      43         2          2          3
#> 166     70     168      71         1          9          4
#> 167     84       9      50         2          1          3
#> 168     82     168      57         2          9          3
#> 169     76      15      52         1          1          3
#> 170     77       6      53         1          1          3
#> 171     66      14      60         1          1          4
#> 172     90      35      65         2          2          4
#> 173     76       7      72         1          1          4
#> 174     69      12      64         1          1          4
#> 175     88      50      57         2          3          3
#> 176     71       6      47         1          1          3
#> 177     93      22      62         2          2          4
#> 178     97      35      67         2          2          4
#> 179     84       6      63         2          1          4
#> 180     79     168      77         1          9          4
#> 181     91      28      60         2          2          4
#> 182     84      10      48         2          1          3
#> 183     75      91      73         1          5          4
#> 184     81       8      25         2          1          2
#> 185     77     168      58         1          9          3
#> 186     75      24      50         1          2          3
#> 187     74     168      45         1          9          3
#> 188     92     168      53         2          9          3
#> 189     79      14      59         1          1          3
#> 190     74      28      80         1          2          5
#> 191     76      16      57         1          1          3
#> 192     NA     168      66        NA          9          4
#> 193     84       6      63         2          1          4
#> 194     65     168      56         1          9          3
#> 195     85      28      61         2          2          4
#> 196     75     160      70         1          9          4
#> 197     90      39      62         2          2          4
#> 198     83      15      47         2          1          3
#> 199     87      10      67         2          1          4
#> 200     86      10      64         2          1          4
#> 201     93      17      65         2          1          4
#> 202     92     168      55         2          9          3
#> 203     92      70      66         2          4          4
#> 204     84      50      52         2          3          3
#> 205     83      12      64         2          1          4
#> 206     82      10      49         2          1          3
#> 207     87     168      42         2          9          3
#> 208     89     168      58         2          9          3
#> 209     91     168      87         2          9          5
#> 210     84      28      65         2          2          4
#> 211     93      12      65         2          1          4
#> 212     85       8      63         2          1          4
#> 213     79      21      43         1          2          3
#> 214     84      24      54         2          2          3
#> 215     65      15      42         1          1          3
#> 216     89       8      61         2          1          4
#> 217     84      16      54         2          1          3
#> 218     81      56      47         2          3          3
#> 219     77      42      46         1          3          3
#> 220     65       4      54         1          1          3
#> 221     93      25      58         2          2          3
#> 222     96      35      64         2          2          4
#> 223     90      30      51         2          2          3
#> 224     86       6      57         2          1          3
#> 225     88     110      63         2          6          4
#> 226     79      30      49         1          2          3
#> 227     91      24      66         2          2          4
#> 228     84      14      56         2          1          3
#> 229     71       4      62         1          1          4
#> 230     67      20      29         1          2          2
#> 231     87      10      55         2          1          3
#> 232     84       7      55         2          1          3
#> 233     87     161      81         2          9          5
#> 234     84      28      62         2          2          4
#> 235     86      14      49         2          1          3
#> 236     83       4      43         2          1          3
#> 237     69      50      60         1          3          4
#> 238     75      10      39         1          1          2
#> 239     79      18      41         1          1          3
#> 240     76      15      25         1          1          2
#> 241     76       6      27         1          1          2
#> 242     82      17      26         2          1          2
#> 243     88      14      52         2          1          3
#> 244     90       5      56         2          1          3
#> 245     87      10      29         2          1          2
#> 246     76      30      45         1          2          3
#> 247     76      10      64         1          1          4
#> 248     86      10      47         2          1          3
#> 249     83      84      62         2          5          4
#> 250     89      10      55         2          1          3
#> 251     82       6      70         2          1          4
#> 252     79      15      51         1          1          3
#> 253     67      40      67         1          3          4
#> 254     73      21      23         1          2          2
#> 255     89      11      52         2          1          3
#> 256     95       8      62         2          1          4
#> 257     65       5      33         1          1          2
#> 258     89      12      83         2          1          5
#> 259     83      10      75         2          1          4
#> 260     86      20      60         2          2          4
#> 261     82      12      63         2          1          4
#> 262     93      24      65         2          2          4
#> 263     90      27      60         2          2          4
#> 264     79      14      54         1          1          3
#> 265     97      18      63         2          1          4
#> 266     78     168      58         1          9          3
#> 267     74      14      42         1          1          3
#> 268     86      10      58         2          1          3
#> 269     88       6      55         2          1          3
#> 270     77      21      44         1          2          3
#> 271     76      16      46         1          1          3
#> 272     74      15      38         1          1          2
#> 273     76      20      44         1          2          3
#> 274     71      10      43         1          1          3
#> 275     74      22      34         1          2          2
#> 276     82      35      43         2          2          3
#> 277     82      30      66         2          2          4
#> 278     74      16      46         1          1          3
#> 279     73      24      68         1          2          4
#> 280     67     168      60         1          9          4
#> 281     70     168      35         1          9          2
#> 282     70      40      38         1          3          2
#> 283     70      40      41         1          3          3
#> 284     67      50      40         1          3          3
#> 285     80      20      39         2          2          2
#> 286     69      20      44         1          2          3
#> 287     89       6      57         2          1          3
#> 288     83       5      48         2          1          3
#> 289     83      48      56         2          3          3
#> 290     67     168      40         1          9          3
#> 291     84       6      61         2          1          4
#> 292     90       8      63         2          1          4
#> 293     80      43      59         2          3          3
#> 294     81      90      69         2          5          4
#> 295     87     100      60         2          6          4
#> 296     73       8      49         1          1          3
#> 297     71      15      41         1          1          3
#> 298     80       5      72         2          1          4
#> 299     69       8      64         1          1          4
#> 300     73      10      46         1          1          3
#> 301     88      30      64         2          2          4
#> 302     73     168      79         1          9          4
#> 303     78      20      39         1          2          2
#> 304     86      30      42         2          2          3
#> 305     75      30      39         1          2          2
#> 306     89      12      42         2          1          3
#> 307     83      30      54         2          2          3
#> 308     78      30      76         1          2          4
#> 309     69      14      69         1          1          4
#> 310     70      15      68         1          1          4
#> 311     86      50      66         2          3          4
#> 312     67      20      35         1          2          2
#> 313     74      25      30         1          2          2
#> 314     87      40      54         2          3          3
#> 315     66       9      56         1          1          3
#> 316     68      18      60         1          1          4
#> 317     72      25      26         1          2          2
#> 318     80     100      68         2          6          4
#> 319     89       6      51         2          1          3
#> 320     84      25      54         2          2          3
#> 321     93      15      52         2          1          3
#> 322     86      20      53         2          2          3
#> 323     84      10      54         2          1          3
#> 324     86      28      52         2          2          3
#> 325     83      10      48         2          1          3
#> 326     86      30      46         2          2          3
#> 327     88       6      68         2          1          4
#> 328     75      40      49         1          3          3
#> 329     76       7      19         1          1          1
#> 330     NA       8      49        NA          1          3
#> 331     81      15      33         2          1          2
#> 332     78       4      42         1          1          3
#> 333     83       4      34         2          1          2
#> 334     94     168      66         2          9          4
#> 335     84       7      27         2          1          2
#> 336     71      22      37         1          2          2
#> 337     86      30      54         2          2          3
#> 338     89      15      65         2          1          4
#> 339     93      30      72         2          2          4
#> 340     75      20      40         1          2          3
#> 341     73      25      26         1          2          2
#> 342     70      20      29         1          2          2
#> 343     75      20      30         1          2          2
#> 344     NA      25      30        NA          2          2
#> 345     75      14      33         1          1          2
#> 346    103      30      42         3          2          3
#> 347     70      20      51         1          2          3
#> 348     84      15      54         2          1          3
#> 349     78       6      38         1          1          2
#> 350     85       5      40         2          1          3
#> 351     81      50      50         2          3          3
#> 352     90       8      66         2          1          4
#> 353     76      20      44         1          2          3
#> 354     75      14      42         1          1          3
#> 355     74      18      43         1          1          3
#> 356     75      16      55         1          1          3
#> 357     89      20      67         2          2          4
#> 358     73      15      23         1          1          2
#> 359     79      25      70         1          2          4
#> 360     83      18      62         2          1          4
#> 361     91      22      66         2          2          4
#> 362     81      18      59         2          1          3
#> 363     77      48      76         1          3          4
#> 364     65      60      44         1          4          3
#> 365     73      10      42         1          1          3
#> 366     73       7      53         1          1          3
#> 367     69       5      44         1          1          3
#> 368     79      35      62         1          2          4
#> 369     82      28      53         2          2          3
#> 370     76       5      58         1          1          3
#> 371     84      25      38         2          2          2
#> 372     86      40      48         2          3          3
#> 373     88      49      52         2          3          3
#> 374     76       4      53         1          1          3
#> 375     94      70      67         2          4          4
#> 376     78       6      54         1          1          3
#> 377     67       7      31         1          1          2
#> 378     81      40      51         2          3          3
#> 379     69      50      48         1          3          3
#> 380     77      30      52         1          2          3
#> 381     73      70      45         1          4          3
#> 382     70      14      47         1          1          3
#> 383     82      70      42         2          4          3
#> 384     93       7      65         2          1          4
#> 385     81       6      49         2          1          3
#> 386     70      14      46         1          1          3
#> 387     78      10      65         1          1          4
#> 388     84      25      47         2          2          3
#> 389     83       5      54         2          1          3
#> 390     82      11      49         2          1          3
#> 391     85     120      47         2          7          3
#> 392     83      18      59         2          1          3
#> 393     68       6      69         1          1          4
#> 394     70      20      34         1          2          2
#> 395     76      15      30         1          1          2
#> 396     73      35      70         1          2          4
#> 397     76      15      32         1          1          2
#> 398     80      15      41         2          1          3
#> 399     67     120      63         1          7          4
#> 400     77      28      83         1          2          5
#> 401     79      10      44         1          1          3
#> 402     72     120      70         1          7          4
#> 403     90     100      56         2          6          3
#> 404     90      18      64         2          1          4
#> 405     66       8      43         1          1          3
#> 406     80      40      53         2          3          3
#> 407     81     140      55         2          8          3
#> 408     85       6      46         2          1          3
#> 409     83      11      64         2          1          4
#> 410     85      15      57         2          1          3
#> 411     84      80      60         2          5          4
#> 412     65       5      42         1          1          3
#> 413     71       6      63         1          1          4
#> 414     68      40      68         1          3          4
#> 415     65       5      67         1          1          4
#> 416     73       7      69         1          1          4
#> 417     68       4      52         1          1          3
#> 418     67      24      60         1          2          4
#> 419     73      28      48         1          2          3
#> 420     84      25      60         2          2          4
#> 421     98     140      67         2          8          4
#> 422     78      14      36         1          1          2
#> 423     82      40      75         2          3          4
#> 424     89       5      29         2          1          2
#> 425     84      20      47         2          2          3
#> 426     70     168      62         1          9          4
#> 427     78      40      59         1          3          3
#> 428     82      80      51         2          5          3
#> 429     94     120      34         2          7          2
#> 430     89     160      59         2          9          3
#> 431     73       7      31         1          1          2
#> 432     80       7      37         2          1          2
#> 433     84       4      61         2          1          4
#> 434     78       6      53         1          1          3
#> 435     83      14      89         2          1          5
#> 436     77       7      53         1          1          3
#> 437     82      28      46         2          2          3
#> 438     81      25      59         2          2          3
#> 439     72      28      74         1          2          4
#> 440     88       7      50         2          1          3
#> 441     80      21      50         2          2          3
#> 442     79      30      53         1          2          3
#> 443     66      25      55         1          2          3
#> 444     86      65      51         2          4          3
#> 445     85      18      43         2          1          3
#> 446     79      40      43         1          3          3
#> 447     84      20      59         2          2          3
#> 448     NA      80      72        NA          5          4
#> 449     87       7      56         2          1          3
#> 450     71      50      66         1          3          4
#> 451     71      30      47         1          2          3
#> 452     76      10      45         1          1          3
#> 453     80       8      NA         2          1         NA
#> 454     81       6      23         2          1          2
#> 455     90       6      25         2          1          2
#> 456     82      42      57         2          3          3
#> 457     77      10      53         1          1          3
#> 458     95      30      64         2          2          4
#> 459     86      15      56         2          1          3
#> 460     82      10      41         2          1          3
#> 461     90      10      58         2          1          3
#> 462     79     130      76         1          7          4
#> 463     95     160      69         2          9          4
#> 464     87     168      56         2          9          3
#> 465     78      24      75         1          2          4
#> 466     83      24      53         2          2          3
#> 467     75      30      53         1          2          3
#> 468     80      20      58         2          2          3
#> 469     72      10      48         1          1          3
#> 470     76       7      35         1          1          2
#> 471     84     120      59         2          7          3
#> 472     66       6      34         1          1          2
#> 473     77       6      39         1          1          2
#> 474     84      10      56         2          1          3
#> 475     68      20      60         1          2          4
#> 476     78       5      43         1          1          3
#> 477     65      60      56         1          4          3
#> 478     72      20      38         1          2          2
#> 479     67       6      54         1          1          3
#> 480     78      10      25         1          1          2
#> 481     67      84      66         1          5          4
#> 482     82       8      58         2          1          3
#> 483     75     168      75         1          9          4
#> 484     82      40      30         2          3          2
#> 485     79      40      42         1          3          3
#> 486     76      20      38         1          2          2
#> 487     89      56      63         2          3          4
#> 488     92       6      57         2          1          3
#> 489     66       7      41         1          1          3
#> 490     89      12      64         2          1          4
#> 491     79      10      32         1          1          2
#> 492     73      14      38         1          1          2
#> 493     89      28      65         2          2          4
#> 494     79      12      31         1          1          2
#> 495     NA      28      43        NA          2          3
#> 496     81      11      29         2          1          2
#> 497     84      16      62         2          1          4
#> 498     77      12      46         1          1          3
#> 499     73      22      43         1          2          3
#> 500     79      17      44         1          1          3
#> 501     73      16      47         1          1          3
#> 502     80      50      49         2          3          3
#> 503     73       6      46         1          1          3
#> 504     88       5      46         2          1          3
#> 505     82      45      60         2          3          4
#> 506     93      16      65         2          1          4
#> 507     70      15      32         1          1          2
#> 508     69      20      72         1          2          4
#> 509     70      20      35         1          2          2
#> 510     86       8      61         2          1          4
#> 511     75       8      66         1          1          4
#> 512     81      15      53         2          1          3
#> 513     65      10      35         1          1          2
#> 514     86      15      57         2          1          3
#> 515     91      27      62         2          2          4
#> 516     72      14      70         1          1          4
#> 517     87      28      53         2          2          3
#> 518     77     168      78         1          9          4
#> 519     65     168      65         1          9          4
#> 520     81      21      58         2          2          3
#> 521     72      12      62         1          1          4
#> 522     93      62      48         2          4          3
#> 523     65      20      51         1          2          3
#> 524     77       7      35         1          1          2
#> 525     77      10      59         1          1          3
#> 526     76      20      42         1          2          3
#> 527     85       8      52         2          1          3
#> 528     79       9      49         1          1          3
#> 529     82      12      46         2          1          3
#> 530     83      10      56         2          1          3
#> 531     86      35      51         2          2          3
#> 532     71       5      45         1          1          3
#> 533     75      18      50         1          1          3
#> 534     83      15      48         2          1          3
#> 535     78       6      41         1          1          3
#> 536     74       8      48         1          1          3
#> 537     88      12      40         2          1          3
#> 538     86      25      29         2          2          2
#> 539     82      10      50         2          1          3
#> 540     89      40      48         2          3          3
#> 541     91      20      63         2          2          4
#> 542     90       6      54         2          1          3
#> 543     77     168      65         1          9          4
#> 544     65       4      52         1          1          3
#> 545     69     168      41         1          9          3
#> 546     68      24      44         1          2          3
#> 547     70      25      53         1          2          3
#> 548     82      40      62         2          3          4
#> 549     77      18      42         1          1          3
#> 550     82      20      31         2          2          2
#> 551     69      40      40         1          3          3
#> 552     81       6      44         2          1          3
#> 553     86       5      50         2          1          3
#> 554     85      30      49         2          2          3
#> 555     67      14      24         1          1          2
#> 556     70       8      43         1          1          3
#> 557     78      28      61         1          2          4
#> 558     83      45      35         2          3          2
#> 559     81      40      40         2          3          3
#> 560     74      30      53         1          2          3
#> 561     78      28      58         1          2          3
#> 562     83      50      48         2          3          3
#> 563     69      84      65         1          5          4
#> 564     97      12      61         2          1          4
#> 565     70      40      31         1          3          2
#> 566     92      10      58         2          1          3
#> 567     91      15      53         2          1          3
#> 568     65      40      63         1          3          4
#> 569     NA      24      80        NA          2          5
#> 570     94     168      61         2          9          4
#> 571     69     160      46         1          9          3
#> 572     78     162      75         1          9          4
#> 573     87     100      49         2          6          3
#> 574     82     110      48         2          6          3
#> 575     84     140      62         2          8          4
#> 576     79      25      49         1          2          3
#> 577     85      15      49         2          1          3
#> 578     65      35      60         1          2          4
#> 579     76      30      27         1          2          2
#> 580     65      40      63         1          3          4
#> 581     78      10      57         1          1          3
#> 582     91      25      56         2          2          3
#> 583     88      12      63         2          1          4
#> 584     83      35      78         2          2          4
#> 585     73      10      36         1          1          2
#> 586     79      30      59         1          2          3
#> 587     86      10      62         2          1          4
#> 588     82     100      42         2          6          3
#> 589     75      12      54         1          1          3
#> 590     66      56      64         1          3          4
#> 591     93      40      65         2          3          4
#> 592     86      30      52         2          2          3
#> 593     86      60      78         2          4          4
#> 594     69     100      47         1          6          3
#> 595     66      20      67         1          2          4
#> 596     83      35      28         2          2          2
#> 597     65     168      43         1          9          3
#> 598     71      22      40         1          2          3
#> 599     83      60      53         2          4          3
#> 600     92      24      28         2          2          2
#> 601     83      20      78         2          2          4
#> 602     95      70      58         2          4          3
#> 603     76      25      43         1          2          3
#> 604     81       6      54         2          1          3
#> 605     79      18      28         1          1          2
#> 606     87      10      57         2          1          3
#> 607     74      15      47         1          1          3
#> 608     70      20      35         1          2          2
#> 609     80      20      30         2          2          2
#> 610     75      10      50         1          1          3
#> 611     68      22      36         1          2          2
#> 612     69      15      38         1          1          2
#> 613     68      48      58         1          3          3
#> 614     70      80      33         1          5          2
#> 615     89      77      59         2          4          3
#> 616     NA       6      30        NA          1          2
#> 617     83       6      24         2          1          2
#> 618     70       8      30         1          1          2
#> 619     82      20      39         2          2          2
#> 620     84      20      40         2          2          3
#> 621     82       8      51         2          1          3
#> 622     80       6      49         2          1          3
#> 623     86       8      60         2          1          4
#> 624     69      15      35         1          1          2
#> 625     87      10      53         2          1          3
#> 626     71      10      35         1          1          2
#> 627     66      15      39         1          1          2
#> 628     85       8      54         2          1          3
#> 629     85       6      65         2          1          4
#> 630     95      56      63         2          3          4
#> 631     76       6      53         1          1          3
#> 632     69       8      64         1          1          4
#> 633     68      28      43         1          2          3
#> 634     82      20      58         2          2          3
#> 635     92      70      67         2          4          4
#> 636     66       8      30         1          1          2
#> 637     74       7      24         1          1          2
#> 638     75      10      56         1          1          3
#> 639     90      10      44         2          1          3
#> 640     72      10      69         1          1          4
#> 641     92     168      31         2          9          2
#> 642     70      40      63         1          3          4
#> 643     79      40      44         1          3          3
#> 644     79      30      42         1          2          3
#> 645     69      30      42         1          2          3
#> 646     70      28      41         1          2          3
#> 647     82      14      77         2          1          4
#> 648     79      40      47         1          3          3
#> 649     72      10      70         1          1          4
#> 650     76       4      22         1          1          2
#> 651     65      14      37         1          1          2
#> 652     92      42      64         2          3          4
#> 653     70      42      50         1          3          3
#> 654     89      65      69         2          4          4
#> 655     72       5      44         1          1          3
#> 656     75      10      47         1          1          3
#> 657     73      45      41         1          3          3
#> 658     82      55      55         2          3          3
#> 659     80      50      60         2          3          4
#> 660     82      35      56         2          2          3
#> 661     82      40      66         2          3          4
#> 662     71      15      46         1          1          3
#> 663     77       8      55         1          1          3
#> 664     73      25      40         1          2          3
#> 665     80       5      51         2          1          3
#> 666     71       5      37         1          1          2
#> 667     65       4      58         1          1          3
#> 668     83      14      57         2          1          3
#> 669     67      20      25         1          2          2
#> 670     68      10      43         1          1          3
#> 671     71      50      69         1          3          4
#> 672     77      16      35         1          1          2
#> 673     72      22      48         1          2          3
#> 674     68     100      66         1          6          4
#> 675     77     100      44         1          6          3
#> 676     70      30      74         1          2          4
#> 677     84     120      77         2          7          4
#> 678     89     100      61         2          6          4
#> 679     68      10      41         1          1          3
#> 680     89     120      64         2          7          4
#> 681     68       6      20         1          1          2
#> 682     72       6      27         1          1          2
#> 683     78       5      53         1          1          3
#> 684     84      10      51         2          1          3
#> 685     92       9      54         2          1          3
#> 686     68       9      69         1          1          4
#> 687     66      12      43         1          1          3
#> 688     71      20      69         1          2          4
#> 689     65      10      70         1          1          4
#> 690     81     160      60         2          9          4
#> 691     82     100      48         2          6          3
#> 692     75      20      33         1          2          2
#> 693     80      56      57         2          3          3
#> 694     70       7      37         1          1          2
#> 695     91      35      43         2          2          3
#> 696     94     120      73         2          7          4
#> 697     66      60      61         1          4          4
#> 698     87      10      54         2          1          3
#> 699     86      14      57         2          1          3
#> 700     83      21      46         2          2          3
#> 701     73      10      54         1          1          3
#> 702     70       7      48         1          1          3
#> 703     73      18      70         1          1          4
#> 704     83       4      49         2          1          3
#> 705     76       8      47         1          1          3
#> 706     90      10      64         2          1          4
#> 707     72       6      48         1          1          3
#> 708     78      10      71         1          1          4
#> 709     71      30      66         1          2          4
#> 710     72      10      56         1          1          3
#> 711     86       9      46         2          1          3
#> 712     70      36      62         1          2          4
#> 713     67      15      49         1          1          3
#> 714     65      35      55         1          2          3
#> 715     74      21      47         1          2          3
#> 716     76     168      47         1          9          3
#> 717     79     168      43         1          9          3
#> 718     66      10      61         1          1          4
#> 719     73       6      45         1          1          3
#> 720     90      20      55         2          2          3
#> 721     68       6      33         1          1          2
#> 722     67       8      46         1          1          3
#> 723     86      15      51         2          1          3
#> 724     79      84      49         1          5          3
#> 725     75      45      48         1          3          3
#> 726     85      40      63         2          3          4
#> 727     77      90      68         1          5          4
#> 728     79      35      52         1          2          3
#> 729     93      70      68         2          4          4
#> 730     80       5      18         2          1          1
#> 731     77      20      52         1          2          3
#> 732     81      35      58         2          2          3
#> 733     80       8      56         2          1          3
#> 734     86      30      47         2          2          3
#> 735     67       4      60         1          1          4
#> 736     91      84      55         2          5          3
#> 737     72      10      68         1          1          4
#> 738     82      25      59         2          2          3
#> 739     94      60      65         2          4          4
#> 740     89       6      64         2          1          4
#> 741     83      26      52         2          2          3
#> 742     75      12      54         1          1          3
#> 743     89      25      50         2          2          3
#> 744     80      15      49         2          1          3
#> 745     84      50      73         2          3          4
#> 746     77      18      44         1          1          3
#> 747     75      12      35         1          1          2
#> 748     79      22      44         1          2          3
#> 749     85      21      50         2          2          3
#> 750     87      70      58         2          4          3
#> 751     82     168      71         2          9          4
#> 752     90       5      60         2          1          4
#> 753     83      30      49         2          2          3
#> 754     85      70      62         2          4          4
#> 755     80     168      59         2          9          3
#> 756     66       9      57         1          1          3
#> 757     79       6      50         1          1          3
#> 758     91       4      64         2          1          4
#> 759     89      20      48         2          2          3
#> 760     69       8      56         1          1          3
#> 761     73      14      52         1          1          3
#> 762     83       9      58         2          1          3
#> 763     79       6      69         1          1          4
#> 764     85      14      44         2          1          3
#> 765     81       7      60         2          1          4
#> 766     85       6      19         2          1          1
#> 767     66      20      63         1          2          4
#> 768     74      20      35         1          2          2
#> 769     73      48      69         1          3          4
#> 770     76      10      44         1          1          3
#> 771     83       5      58         2          1          3
#> 772     78       6      42         1          1          3
#> 773     88       8      65         2          1          4
#> 774     89       8      53         2          1          3
#> 775     84       8      53         2          1          3
#> 776     73       8      41         1          1          3
#> 777     92       4      67         2          1          4
#> 778     65       4      33         1          1          2
#> 779     69       8      39         1          1          2
#> 780     77      25      73         1          2          4
#> 781     79      20      56         1          2          3
#> 782     73       6      50         1          1          3
#> 783     68      21      45         1          2          3
#> 784     69      28      51         1          2          3
#> 785     80      70      49         2          4          3
#> 786     81       8      34         2          1          2
#> 787     67      21      47         1          2          3
#> 788     74      28      50         1          2          3
#> 789     88      14      49         2          1          3
#> 790     70       4      37         1          1          2
#> 791     73       5      46         1          1          3
#> 792     67       6      52         1          1          3
#> 793     74       7      70         1          1          4
#> 794     87     140      62         2          8          4
#> 795     91       4      23         2          1          2
#> 796     76       8      22         1          1          2
#> 797     87       7      21         2          1          2
#> 798     72       8      48         1          1          3
#> 799     77       7      52         1          1          3
#> 800     89      30      41         2          2          3
#> 801     84      40      68         2          3          4
#> 802     78      50      32         1          3          2
#> 803     81      35      50         2          2          3
#> 804     74      42      68         1          3          4
#> 805     80      40      55         2          3          3
#> 806     77     168      70         1          9          4
#> 807     79     150      69         1          8          4
#> 808     89     168      63         2          9          4
#> 809     90     168      77         2          9          4
#> 810     87     168      77         2          9          4
#> 811     81      14      29         2          1          2
#> 812     82       6      33         2          1          2
#> 813     71       8      43         1          1          3
#> 814     75      22      54         1          2          3
#> 815     69      10      38         1          1          2
#> 816     77       6      45         1          1          3
#> 817     81      30      70         2          2          4
#> 818     87       8      53         2          1          3
#> 819     73      10      53         1          1          3
#> 820     65      10      35         1          1          2
#> 821     68      12      42         1          1          3
#> 822     72      20      40         1          2          3
#> 823     70      15      36         1          1          2
#> 824     68      20      32         1          2          2
#> 825     70      15      39         1          1          2
#> 826     68      20      40         1          2          3
#> 827     69      25      35         1          2          2
#> 828     78       7      49         1          1          3
#> 829     74       4      47         1          1          3
#> 830     68      20      44         1          2          3
#> 831     86      10      38         2          1          2
#> 832     79      50      70         1          3          4
#> 833     75      20      55         1          2          3
#> 834     85      25      52         2          2          3
#> 835     71       5      48         1          1          3
#> 836     74       7      47         1          1          3
#> 837     74       5      62         1          1          4
#> 838     86      12      57         2          1          3
#> 839     66      35      20         1          2          2
#> 840     70      35      29         1          2          2
#> 841     86      30      33         2          2          2
#> 842     85      40      60         2          3          4
#> 843     82      15      58         2          1          3
#> 844     79      10      44         1          1          3
#> 845     69       8      43         1          1          3
#> 846     67       5      41         1          1          3
#> 847     94      14      56         2          1          3
#> 848     75      14      48         1          1          3
#> 849     79       6      47         1          1          3
#> 850     78      14      44         1          1          3
#> 851     66       7      39         1          1          2
#> 852     65      20      31         1          2          2
#> 853     88       6      47         2          1          3
#> 854     78      10      35         1          1          2
#> 855     83      30      46         2          2          3
#> 856     87      14      31         2          1          2
#> 857     99      10      44         2          1          3
#> 858     66      10      34         1          1          2
#> 859     89      28      58         2          2          3
#> 860     71       8      37         1          1          2
#> 861     82       6      43         2          1          3
#> 862     69      20      45         1          2          3
#> 863     81       5      54         2          1          3
#> 864     84       7      57         2          1          3
#> 865     90      20      57         2          2          3
#> 866     80      18      28         2          1          2
#> 867     79      12      62         1          1          4
#> 868     67      17      64         1          1          4
#> 869     71      14      48         1          1          3
#> 870     75       5      38         1          1          2
#> 871     68       6      38         1          1          2
#> 872     75       4      33         1          1          2
#> 873     73       4      49         1          1          3
#> 874     81       6      42         2          1          3
#> 875     76      10      55         1          1          3
#> 876     65       6      45         1          1          3
#> 877     83      14      68         2          1          4
#> 878     79      30      39         1          2          2
#> 879     73      35      79         1          2          4
#> 880     78      10      48         1          1          3
#> 881     83     100      49         2          6          3
#> 882     74       8      34         1          1          2
#> 883     81      10      53         2          1          3
#> 884     87      20      65         2          2          4
#> 885     67      40      39         1          3          2
#> 886     79      40      74         1          3          4
#> 887     81      40      76         2          3          4
#> 888     70       6      41         1          1          3
#> 889     65      30      61         1          2          4
#> 890     93      28      61         2          2          4
#> 891     74      20      40         1          2          3
#> 892     77      25      45         1          2          3
#> 893     75      30      36         1          2          2
#> 894     90      50      60         2          3          4
#> 895     91      20      50         2          2          3
#> 896     86      15      55         2          1          3
#> 897     65     110      38         1          6          2
#> 898     82      28      82         2          2          5
#> 899     80      85      36         2          5          2
#> 900     74     160      71         1          9          4
#> 901     65      10      44         1          1          3
#> 902     67       8      45         1          1          3
#> 903     NA      NA      NA        NA         NA         NA
#> 904     NA      NA      NA        NA         NA         NA
#> 905     NA      NA      NA        NA         NA         NA
#> 906     NA      NA      NA        NA         NA         NA
#> 907     NA      NA      NA        NA         NA         NA
#> 908     NA      NA      NA        NA         NA         NA

# create vector with values from 50 to 80
dummy <- round(runif(200, 50, 80))
# labels with grouping starting at lower bound
group_labels(dummy)
#> [1] "50-54" "55-59" "60-64" "65-69" "70-74" "75-79" "80-84"
# labels with grouping startint at upper bound
group_labels(dummy, right.interval = TRUE)
#> [1] "46-50" "51-55" "56-60" "61-65" "66-70" "71-75" "76-80"

# works also with gouped data frames
mtcars %>%
  group_var(disp, size = 4, append = FALSE) %>%
  table()
#> disp_gr
#>  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 
#>  1  1  2  1  1  3  1  2  2  2  1  1  3  1  1  1  2  2  1  1  1  1 

mtcars %>%
  group_by(cyl) %>%
  group_var(disp, size = 4, append = FALSE) %>%
  table()
#> disp_gr
#>  1  2  3  4  5  6  7  8  9 10 
#>  5  4  5  3  4  5  2  2  1  1