简体   繁体   English

根据一列中的值对R数据框中的行进行排序

[英]ordering rows in an R data frame based on value in one column

My data set is as follows 我的数据集如下

dput(data2)
structure(list(School = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 
3L, 3L), .Label = c("School1", "School2", "School3"), class = "factor"), 
    Year = c(2015L, 2014L, 2013L, 2015L, 2014L, 2013L, 2015L, 
    2014L, 2013L), Rate = c(70L, 50L, 30L, 80L, 90L, 11L, 60L, 
    50L, 40L)), .Names = c("School", "Year", "Rate"), class = "data.frame", row.names = c(NA, 
-9L))


   School Year Rate
1 School1 2015   70
2 School1 2014   50
3 School1 2013   30
4 School2 2015   80
5 School2 2014   90
6 School2 2013   11
7 School3 2015   60
8 School3 2014   50
9 School3 2013   40

What I am trying do is produce an output where the data is grouped by column School and the order for the schools is defined on the descending order of rate in Year 2015. 我正在尝试生成一个输出,其中数据按“学校”列进行分组,并且学校的顺序是按2015年汇率的降序定义的。

So the output should be like 所以输出应该像

   School Year Rate
1 School2 2015   80
2 School2 2014   90
3 School2 2013   11
4 School1 2015   70
5 School1 2014   50
6 School1 2013   30
7 School3 2015   60
8 School3 2014   50
9 School3 2013   40

Using the data in my example the order will be as follows, based on the descending value of rate. 在示例中,使用数据的顺序将基于rate的降序如下。 School2 -> School1 -> School3 80 -> 70 -> 60 学校2->学校1->学校3 80-> 70-> 60

I have tried using dplyr package to get the desired output but have not been achieve the result. 我尝试使用dplyr软件包来获得所需的输出,但尚未实现结果。

We can find the rate in 2015 and then arrange based on the columns. 我们可以找到2015年的汇率,然后根据各列进行排列。

library(dplyr)

dat2 <- dat %>%
  group_by(School) %>%
  mutate(Year2015 = Rate[Year == 2015]) %>%
  arrange(desc(Year2015), desc(Year)) %>%
  ungroup(School) %>%
  select(-Year2015)
dat2
# # A tibble: 9 x 3
#   School   Year  Rate
#   <fct>   <int> <int>
# 1 School2  2015    80
# 2 School2  2014    90
# 3 School2  2013    11
# 4 School1  2015    70
# 5 School1  2014    50
# 6 School1  2013    30
# 7 School3  2015    60
# 8 School3  2014    50
# 9 School3  2013    40

An option to calculate maximum rate ( MaxRate ) for each school first. 一个选项,用于首先计算每个学校的最大费率( MaxRate )。 Then arrange data in descending order on MaxRate and Year . 然后在MaxRateYear上按降序排列数据。

library(dplyr)

data2 %>% group_by(School) %>%
  mutate(MaxRate = max(Rate)) %>%
  arrange(desc(MaxRate), desc(Year)) %>%
  ungroup() %>%
  select(-MaxRate) %>% as.data.frame()

# School Year Rate
# 1 School2 2015   80
# 2 School2 2014   90
# 3 School2 2013   11
# 4 School1 2015   70
# 5 School1 2014   50
# 6 School1 2013   30
# 7 School3 2015   60
# 8 School3 2014   50
# 9 School3 2013   40

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM