merge_levels: Merge specific levels of a factor

Description Usage Arguments Details Value See Also Examples

Description

This function allows the user to programatically merge levels of a factor together to create aggregate levels. This is designed to work for data that has been discretized by, e.g. vector_bin, but will work for arbitrary factors. In the case of factors with numeric levels, the user can specify to merge the names of the old levels when naming the new level

Usage

1
merge_levels(var, lv, merge_names = FALSE)

Arguments

var

Vector for which is.factor returns TRUE

lv

Character vector containing 2 or more levels of var to merge.

merge_names

Logical. Should names of numeric factors be merged? Default is FALSE.

Details

When not merging names, if levels A, B, and C are to be merged, the resulting level will be AxBxC. If the factor levels contain numbers, specifying merge_names = TRUE will cause merge_levels to call get_vector_cutpoints on the new level, and then use only the minimum and maximum cutpoints- this will result in the merge of [1,2], [2,3], and [3,4] being named [1,4] instead of [1,2]x[2,3]x[3,4]. While this seems like obviously what one would want the default behaviour to be, the user must specify this explicitly to avoid unexpected results.

Value

A new factor level corresponding to var, with values that were in lv replaced with a new factor level. The name of thre level depends on merge_names.

See Also

woe_single

Examples

1
2
3
4
5
x <- factor(1:10)
merge_levels(x,c(1,2,3))
merge_levels(x,c(1,2,3),merge_names = TRUE)

y <- modellingTools::vector_bin(x,bins = 3)

awstringer/innR2binnR documentation built on May 11, 2019, 4:11 p.m.