是否有R函数可以按其他列条件对日期/时间进行排名?

时间:2019-05-29 17:13:36

标签: r sorting date rank

我有兴趣将dataf中的date列更改为与results $ order中的id对应的有序数字(最早日期= 1,第二最早日期= 2 ...依此类推)。如果一个ID仅显示一次,我希望顺序为1。

date=c("2012-02-18", "2013-03-01", "2013-04-11", "2013-06-06", "2013-09-20", "2013-07-02")
datef=strptime(date, format="%Y-%m-%d")
dataf=data.frame(id=c(20, 20, 20, 21, 21, 22), 
              date=datef, 
              service=c("web", "phone", "person", "phone", "web", "web"))
> dataf
  id       date service
1 20 2012-02-18     web
2 20 2013-03-01   phone
3 20 2013-04-11  person
4 21 2013-06-06   phone
5 21 2013-09-20     web
6 22 2013-07-02     web

我甚至很难找到正确的措辞来寻找解决这个难题的答案。我要胁迫吗?或索引? dataf $ dates放入下面的results $ order?

results=data.frame(id=c(20, 20, 20, 21, 21, 22), 
                   order=c(1,2,3,1,2,1), 
                   service=c("web", "phone", "person", "phone", "web", "web"))

> results
  id order service
1 20     1     web
2 20     2   phone
3 20     3  person
4 21     1   phone
5 21     2     web
6 22     1     web

2 个答案:

答案 0 :(得分:1)

使用dplyr

library(dplyr)
dataf %>% group_by(id) %>% mutate(order = rank(date))
# # A tibble: 6 x 4
# # Groups:   id [3]
#      id date                service order
#   <dbl> <dttm>              <fct>   <dbl>
# 1    20 2012-02-18 00:00:00 web         1
# 2    20 2013-03-01 00:00:00 phone       2
# 3    20 2013-04-11 00:00:00 person      3
# 4    21 2013-06-06 00:00:00 phone       1
# 5    21 2013-09-20 00:00:00 web         2
# 6    22 2013-07-02 00:00:00 web         1

答案 1 :(得分:0)

使用data.table

library(data.table)

setDT(dataf)

setorder(dataf, id, date)
dataf[, order := 1:.N, by = id]
> dataf
   id       date service order
1: 20 2012-02-18     web     1
2: 20 2013-03-01   phone     2
3: 20 2013-04-11  person     3
4: 21 2013-06-06   phone     1
5: 21 2013-09-20     web     2
6: 22 2013-07-02     web     1