Mix values of factor variable levels in (approximately) equal proportions to disrupt existing structure of levels, i.e. "ungroup".

unGroup(gr, n = NULL, base = "new_group_", new.names = NULL)

Arguments

gr

A vector indicating original groups.

n

Number of new groups. If NULL, default number is used which is either nlevels(gr) or nlevels(gr)+1, if NA's are presented (see "Details" section).

base

Base for names of new groups. Default is "new_group_".

new.names

Labels for new groups. If value is not NULL, n and base are ignored.

Value

A vertor with new groups, which are a mix of proportionally distributed (ungrouped) original groups.

Details

Default number of new groups n is equal to number of factor levels. n = nlevels(gr). If NA's exist, they are treated as one more additional group thus n = nlevels(gr) + 1.

See also

Other spHelper utilities: IQR_outliers(), expr2text(), mad_outliers(), rmExpr(), sd_outliers(), subt(), uncall()

Author

Vilmantas Gegzna

Examples

gr <- Scores2$gr

# -------------------------------------------------------
set.seed(1)
newGr <- unGroup(gr)
table(gr,newGr, useNA = "ifany")
#>    newGr
#> gr  new_group_1 new_group_2 new_group_3
#>   A          18          17          17
#>   B          19          18          18
#>   C          14          15          14

# NA's are treated as an additional level ---------------
gr[5:10] <- NA

set.seed(1)
newGr <- unGroup(gr)
table(gr,newGr, useNA = "ifany")
#>       newGr
#> gr     new_group_1 new_group_2 new_group_3 new_group_4
#>   A             13          13          13          13
#>   B             12          12          12          13
#>   C             11          11          11          10
#>   <NA>           2           1           1           2