0

I have been doing some hierarchical clusterings in R. Its worked out fine up til now, producing hclust objects left and center, but suddenly not anymore. Now it will only produce lists when performing:

mydata.clusters <- hclust(dist(mydata[, 1:8]))
mydata.clustercut <- cutree(mydata.clusters, 4)

and when trying to:

table(mydata.clustercut, mydata$customer_lifetime) 

it doesnt produce a table, but an endless print of the values (Im guessing from the list).

Ichibichi
  • 3
  • 2
  • 3
    Welcome to Stackoverflow!, could you look here [how to post a reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and ensure your example your reproducible. You need not post the entire dataset just the subset which replicates your issue. – Silence Dogood May 24 '17 at 10:13

1 Answers1

0

The cutree function provide the grouping to which each observation belong to. For example:

iris.clust <- hclust(dist(iris[,1:4]))
iris.clustcut <- cutree(iris.clust, 4)

iris.clustcut
# [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2
# [52] 2 2 3 2 3 2 3 2 3 3 3 3 2 3 2 3 3 2 3 2 3 2 2 2 2 2 2 2 3 3 3 3 2 3 2 2 2 3 3 3 2 3 3 3 3 3 2 3 3 2 2
# [103] 4 2 2 4 3 4 2 4 2 2 2 2 2 2 2 4 4 2 2 2 4 2 2 4 2 2 2 4 4 4 2 2 2 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2

Additional comparison can then be done by using this as a grouping variable for the observed data:

new.iris <- data.frame(iris, gp=iris.clustcut)

# example to visualise quickly the Species membership of each group 

library(ggplot2)
ggplot(new.iris, aes(gp, fill=Species)) + 
    geom_bar()

enter image description here

Adam Quek
  • 6,973
  • 1
  • 17
  • 23
  • For me, this procudes a plot of how many there is in each cluster. Doesnt matter which variabel I try to "fill". – Ichibichi May 26 '17 at 07:49