{"word_count": 343, "character_count": 2407}
Last year, in a post, I discussed how to merge levels of factor variables, using combinatorial techniques (it was for my STT5100 cours, and trees are not in the syllabus), with an extension on trees at the end of the post.consider the following (simulated dataset)n=200set.seed(1)x1=runif(n)x2=runif(n)y=1+2*x1-x2+rnorm(n,0,.2)LB=sample(LETTERS[1:10])b=data.frame(y=y,x1=x1,  x2=cut(x2,breaks=  c(-1,.05,.1,.2,.35,.4,.55,.65,.8,.9,2),  labels=LB))str(b)'data.frame':200 obs. of  3 variables: $ y : num  1.345 1.863 1.946 2.481 0.765 ... $ x1: num  0.266 0.372 0.573 0.908 0.202 ... $ x2: Factor w/ 10 levels "I","A","H","F",..: 4 4 6 4 3 6 7 3 4 8 ...table(b$x2)[LETTERS[1:10]]  A  B  C  D  E  F  G  H  I  J 11 12 23 34 23 36 12 32  3 14Just by looking at the data (see the previous post), we could easily get the feeling that 10 levels was too much.Following my post, Przemyslaw sent a comment suggesting to uselibrary(factorMerger)It is indeed a nice package (unless you have really really big datasets with a lot of categories in your factor variables - as I experienced recently), and you can get great graphsMF = mergeFactors(response = b$y,              factor = b$x2,              family = "gaussian")plot(MF)Here is suggests to create three categories. Recall that with student t-tests (changing the reference), we gotAnother interesting package, by Piro Polo, islibrary(tree.bins)To use it, we simply call the following function, and we transform automatically our dataset : the continuous variables remain unchanged, and (possibly) categories of categorical variables are mergedb.bins = tree.bins(data=b, y=y)str(b.bins)Classes ‘data.table’ and 'data.frame':200 obs. of  3 variables: $ y : num  1.345 1.863 1.946 2.481 0.765 ... $ x1: num  0.266 0.372 0.573 0.908 0.202 ... $ x2: chr  "Group.4" "Group.4" "Group.4" "Group.4" ... - attr(*, ".internal.selfref")= table(b.bins$x2)Group.1 Group.2 Group.3 Group.4      23      35      26     116here in four groups. To get the correspondance, usetree.bins(data=b, y=y, return = "lkup.list")[[1]]   x2 Categories1   E    Group.12   G    Group.23   C    Group.24   B    Group.35   J    Group.36   I    Group.47   A    Group.48   H    Group.49   F    Group.410  D    Group.4(we have a list with one element, one dataframe, since there is only one factor variable). Cool, isn't it ? I miss Przemyslaw's plot, but this is rather quick, and efficient..&#160;