Created
August 25, 2014 16:07
-
-
Save Oreotrephes/2fe218c11ec86580f68c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #It aint pretty but it works | |
| ################################################################################################################################### | |
| classify <- function(dendrogram,height){ | |
| #this little function will return tip lables | |
| members <- function(n) { | |
| labels<-c() | |
| if (is.leaf(n)) { | |
| a <- attributes(n) | |
| labels<-c(labels,a$label) | |
| } | |
| labels | |
| } | |
| uplow <- cut(dendrogram,height) #the cut dendrogram object | |
| branchesvector<-c() | |
| membersvector<-c() | |
| for(i in 1:length(uplow$lower)){ #for each lower tree resulting from the cut | |
| memlist <- unlist(dendrapply(uplow$lower[[i]],members)) #get the tip lables | |
| branchesvector <- c(branchesvector,rep(i,length(memlist))) #add the lower tree identifier to a vector | |
| membersvector <- c(membersvector,memlist) #add the tip labels to a vector | |
| } | |
| out<-as.integer(branchesvector) #make the output a list of named intergers, to match cut() output | |
| names(out)<-membersvector | |
| out | |
| } | |
| ################################################################################################################################### | |
| ################################################################################################################################### | |
| #demo on question that makes it clear the problem is that cut works alphabetically while cutree works left to right on tree | |
| hc <- hclust(dist(USArrests), "ave") | |
| dend1 <- as.dendrogram(hc) | |
| classify(dend1,70) | |
| cutree(hc,h=70) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment