match and add the cluster number to the original data
Asked Answered
A

1

1

I am using the regular method to do a Hierarchical Clustering project.

mydata.dtm <- TermDocumentMatrix(mydata.corpus)
mydata.dtm2 <- removeSparseTerms(mydata.dtm, sparse=0.98)
mydata.df <- as.data.frame(inspect(mydata.dtm2))
mydata.df.scale <- scale(mydata.df)
d <- dist(mydata.df.scale, method = "euclidean") # distance matrix
fit <- hclust(d, method="ward")
groups <- cutree(fit, k=10)

groups
              congestion        cough          ear          eye        fever          flu   fluzonenon     medicare painpressure     physical         pink          ppd     pressure 
                       1            2            3            4            5            6            5            5            5            7            4            8            5 
                    rash    screening         shot        sinus         sore       sports     symptoms       throat          uti 
                       5            5            6            1            9            7            5            9           10 

And I what I want is to put the group number back to the new column in the original data. I've looked at approximate string matching within single list - r Because the df here is a document matrix so what I got after df <- t(data.frame(mydata.df.scale,cutree(hc,k=10))) is a matrix like

df[1:5,1:5]
     congestion cough ear eye fever
[1,]          0     0   0   0     0
[2,]          0     0   0   0     0
[3,]          0     0   0   0     0
[4,]          0     0   0   1     0
[5,]          0     0   0   0     0

Since eye has the group number 3 then I want add the number 3 to the new column in 4th row.

note that in this case a single document can be mapped to two items in the same group.

df[23,17:21]
   sinus     sore   sports symptoms   throat 
       0        1        0        0        1 
Autocratic answered 7/2, 2015 at 3:7 Comment(2)
It's inelegant, but could you not do something like: ifelse[df$eye == "1", "3", "0")? Repeat for each column but change the replacement number accordingly. Better would be a use of sapply and a lookup table, but I haven't worked that out.Fertility
@Fertility thanks, I did it in a less elegant way.Autocratic
A
0

Instead of put back the number directly I use the 0-1 matrix:

label_back <-t(data.frame(mydata.df,cutree(fit,k=10))) 
row.names(label_back) <- NULL

#label_back<-label_back[1:(nrow(label_back)-1),]# the last line is the sum
groups.df<-as.data.frame(groups)
groups.df$label<-rownames(groups.df)

for (i in 1:length((colnames(label_back)))){
ind<-which(colnames(label_back)[i]==groups.df$label) # match names and return index
label_back[,i]=groups.df$groups[ind]*label_back[,i]  # time the 0-1 with the #group number
     }

find the max value in each row because there are more than 1 value in some rows.

data_group<-rep(0,nrow(data)

for (i in 1:nrow(data)){
  data_group[i]<-max(unique(label_back[i,]))
}
data$group<-data_group

I am looking for more elegant way.

Autocratic answered 7/2, 2015 at 17:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.