-1

Please refer to the attached code:

train <- read.csv("~/Desktop/R/2014data.csv")
d <- dist(train, method = "euclidean") # distance matrix
fit <- hclust(d, method="ward") 
plot(fit) 

Here is the output figure:

enter image description here

How to cut this dendrogram in 4-5 layers and show the output (texts)?

Sumedh
  • 4,835
  • 2
  • 17
  • 32
Norman
  • 121
  • 6

1 Answers1

0

You can use the cuttree() function to trim the tree to a specified depth i.e. 4 or 5. Then to show the tree in text simply print(tree).

# As you had
train <- read.csv("~/Desktop/R/2014data.csv")
d <- dist(train, method = "euclidean") # distance matrix
fit <- hclust(d, method="ward") 
plot(fit)

# trim the tree, k for number of groups, h for height
fit.4 = cuttree(fit, h=4)
fit.5 = cuttree(fit, h=5)

# print the text or plot
print(fit.4)
print(fit.4)
plot(fit.4)
plot(fit.5)
vincentmajor
  • 1,076
  • 12
  • 20