我想按组(多列)对多行进行排名
测试数据如下
data <- structure(list(CODE = c("AA","AA","AA","AA","AA"),
DATE = c(20181205,20181205,20181205,20181212,20181212),
TIME = c(14.0,14.0,14.2,18.0,18.0),
NO = c("D1","D1","D2","D3","D3"),
ITEM = c("AK011","ZH002","HJ005","AK011","KK021"),
RANK = c(1,1,2,3,3)),
.Names = c("CODE", "DATE", "TIME", "NO", "ITEM","RANK"),
class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L))
预期结果
我希望获得的结果是上面用黄色突出显示的结果(排名列), 我想按(CODE,DATE,TIME&NO)列进行排名。
我确实尝试查找并尝试了一些示例R data frame rank by groups (group by rank) with package dplyr 和How to rank within groups in R?,但是所有这些示例都无法对重复的行进行排名。
library(dplyr)
data %>%
group_by(CODE, DATE, TIME, NO) %>%
slice(which.min(TIME))
data %>%
group_by(CODE, DATE, TIME, NO) %>%
mutate(rank = rank(TIME, ties.method="first"))
有什么主意吗?谢谢!