clust <- read.table("W:/R/task9data.txt",header=F, sep=",") # Give row names and column names cnames <- c("country","wine","heart") colnames(clust) <- cnames rownames(clust) <-clust$country # Hierarchical Clustering # Create distance matrix d <- dist(clust, method = "euclidean") fit <- hclust(d) # display dendogram plot(fit) groups <- cutree(fit, k=5) # cut tree into 5 clusters # draw dendogram with red borders around the 5 clusters rect.hclust(fit, k=5, border="red") plot(clust$wine,clust$heart) text(x=clust$wine, y=clust$heart, labels=clust$country, col=groups) # K means clustk <- kmeans(clust[,c("wine","heart")], centers=3, nstart=10) clustk plot(clust$wine, clust$heart, xlab="wine", ylab="heart") text(x=clust$wine, y=clust$heart, labels=clust$country,col=clustk$cluster+1)