match and add the cluster number to the original data

我与影子孤独终老i 提交于 2019-12-08 13:50:37

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!