R 根据两列的组和比率对因子重新排序-按要重新排序的因子分组

R 根据两列的组和比率对因子重新排序-按要重新排序的因子分组,r,plyr,r-factor,R,Plyr,R Factor,我有一个数据帧,df: District TypeofSchool Nstudents Nteachers Percent_failure 1 A I 1936 157 21.5 2 A II 67 8 0.5 3 A III 5288 146 78.

我有一个数据帧,df:

  District TypeofSchool Nstudents Nteachers Percent_failure
1        A            I      1936       157            21.5
2        A           II        67         8             0.5
3        A          III      5288       146            78.0
4        B            I       653        72            27.8
5        B           II       865        22             9.0
6        B          III      2278       100            63.2
对于使用ggplot2作图,我想记录地区因素。我想按该地区的学生与教师比率排序,即将该地区所有类型学校的学生人数和教师人数相加,然后取该比率;按该比率对区域进行重新排序,以便在绘制堆叠条形图时,比率最低的区域将显示在最左侧的位置:

ggplot(df, aes(x=District, y=Percent_failure, fill=TypeofSchool)) +
  geom_bar(stat="identity")

有没有关于如何重新排序的建议?

这里有一种方法可以使用
数据查看。表

require(data.table)
setDT(df)

df[ , ST.RAT := sum(Nstudents)/sum(Nteachers), by = District][order(ST.RAT)]
df[ , District := factor(District,levels=unique(as.character(District)))]
然后进行
ggplot
操作。

基本R解决方案(使用
dat
作为数据帧)

stu.tea带dplyr:

dat = dat %>% group_by(District) %>% mutate(RST=sum(Nstudents/sum(Nteachers))) %>% 
arrange(RST)

dat$District = factor(dat$District,levels(dat$District)[unique(dat$District)])

另一个dplyr解决方案:

df <- df %>% 
  group_by(District) %>% 
  mutate(RST=sum(Nstudents/sum(Nteachers))) %>% 
  arrange(RST) %>%
  mutate(District = factor(District,District)) # the factor levels are reset here
df%
组别(地区)%>%
突变(RST=sum(Nstudents/sum(Nteachers))%>%
排列(RST)%>%
变异(地区=系数(地区,地区))#系数级别在此处重置

请注意,最后一行是通过当前的
df
顺序设置因子级别的顺序,该顺序由
arrange

设置。factor()在data.table中的行为是否不同?不只是按字母顺序排列等级吗?谢谢@Arun。代码是在我发表评论后编辑的。
df <- df %>% 
  group_by(District) %>% 
  mutate(RST=sum(Nstudents/sum(Nteachers))) %>% 
  arrange(RST) %>%
  mutate(District = factor(District,District)) # the factor levels are reset here