可以将Sum_Waste
和Mean_Waste
与您的df_Q1
数据框合并为最终表。
我不确定您要考虑的输出是什么,但是使用kableExtra
的一种方法将合并具有指定列中相同值的单元格。希望这会有所帮助。
编辑:在表中添加了properties
。现在,行按群集和属性排序。
library(kableExtra)
#database
df<-structure(list(Latitude = c(-23.8,-23.8,-23.9,+ -23.9,-23.9),Longitude = c(-49.6,-49.6,-49.7,+ -49.7,-49.6),Waste = c(526,350,526,469,285,175,364,+ 175,45.5,54.6)),class = "data.frame",row.names = c(NA,-19L))
Q1<-matrix(quantile(df$Waste,probs = 0.25))
df_Q1<-subset(df,Waste>Q1[1])
df_Q1
#cluster
d<-dist(df_Q1)
fit.average<-hclust(d,method="average")
clusters<-cutree(fit.average,k=4)
df_Q1$cluster<-clusters
df_Q1$properties<-names(clusters)
#calculate sum waste
dc<-aggregate(df_Q1[,"Waste"],list(cluster=clusters),sum)
colnames(dc)<-c("cluster","Sum_Waste")
head(dc)
#calculate mean waste
dd<-aggregate(df_Q1[,mean)
colnames(dd)<-c("cluster","Mean_Waste")
head(dd)
#merge everything
df_table <- Reduce(merge,list(df_Q1,dc,dd))
#make table
kable(df_table[order(df_table$cluster,as.numeric(df_table$properties)),c(5,2,3,4,1,6,7)],align = "c",row.names = FALSE) %>%
kable_styling(full_width = FALSE) %>%
column_spec(1,bold = TRUE) %>%
collapse_rows(columns = 5:7,valign = "middle")
编辑(4/18/20)
要将properties
汇总为一行,并以逗号分隔,请尝试:
#sort properties
df_table <- df_table[order(df_table$cluster,]
#second table aggregated properties
df_table2 <- aggregate(. ~ cluster + Sum_Waste + Mean_Waste,df_table[,c(1,5,toString)
#make table with df_table2
kable(df_table2[order(df_table2$cluster),c(4,3)],row.names = FALSE) %>%
kable_styling(full_width = FALSE)
本文链接:https://www.f2er.com/2560270.html