Convenience function to make it easy to combine multiple levels

combine_factor(fac, variable=levels(fac), other.label="Other")

Author

Hadley Wickham <h.wickham@gmail.com>

Arguments

fac

factor variable

variable

either a vector of . See examples for more details.

other.label

label for other level

Examples

df <- data.frame(a = LETTERS[sample(5, 15, replace=TRUE)], y = rnorm(15))  
combine_factor(df$a, c(1,2,2,1,2))
#> Warning: NAs introduced by coercion
#>  [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#> Levels: 
combine_factor(df$a, c(1:4, 1))
#> Warning: NAs introduced by coercion
#>  [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
#> Levels: 
(f <- reorder(df$a, df$y))
#>  [1] D D C B C B B C D B A C A B A
#> attr(,"scores")
#>         A         B         C         D 
#> 0.7136232 0.2142761 0.5700412 0.4313978 
#> Levels: B D C A
percent <- tapply(abs(df$y), df$a, sum)
combine_factor(f, c(order(percent)[1:3]))
#>  [1] C     C     A     B     A     B     B     A     C     B     Other A    
#> [13] Other B     Other
#> Levels: B C A Other