maxlevels function

Remove factors with too many levels

Remove factors with too many levels

Removes all columns of a data frame where a factor (or character string) has more than a maximum number of levels.

maxlevels(data, maxlevels = 20, na.omit = TRUE)

Arguments

  • data: data frame which contains the data.
  • maxlevels: number of maximum factor levels.
  • na.omit: logical value whether missing values should be treated as a level, defaults to omit missing values before counting.

Returns

A data frame.

Details

Often categories that have very many levels are not useful in modelling OneR rules because they result in too many rules and tend to overfit. Examples are IDs or names.

Character strings are treated as factors although they keep their datatype. Numeric data is left untouched. If data contains unused factor levels (e.g. due to subsetting) these are ignored and a warning is given.

Examples

df <- data.frame(numeric = c(1:26), alphabet = letters) str(df) str(maxlevels(df))

References

https://github.com/vonjd/OneR

See Also

OneR

Author(s)

Holger von Jouanne-Diedrich

  • Maintainer: Holger von Jouanne-Diedrich
  • License: MIT + file LICENSE
  • Last published: 2017-05-05