R -- 数据分析,表格制作,excel, table

data(“PlantGrowth”) 数据
第一步:生成分类变量
PG= PlantGrowth%>%
mutate(weight_cat=case_when(
weight<=4.5~”light”,
weight>4.5&weight<=5.5~”medium”,
weight>5.5~”heavy”
))
第二步:制表
tab_count1=table(PG$group,PG$weight_cat)
第三步:增加表总数的行列
tab_count2=addmargins(tab_count1,1) 1为行,2为列
第四步:生成百分比
tab_per1=prop.table(tab_count2,1)*100
tab_count3=addmargins(tab_per1,2
tab_per2=addmargins(tab_per1,2)
第五步:将tab_count3与tab_per2合成一个表格
tab_combine=matrix(
pasteq(tab_count3,”(“,round(tab_per2,2),”%”),
dim(tab_count3),
dimnames=dimnames(tab_count3)
)