简体   繁体   English

按组中出现的顺序排列值

[英]Rank values in order of occurence within groups

I am currently trying to rank column values within groups of IDs in the order that they occur. 我目前正在尝试按照ID的顺序对ID组中的列值进行排名。 My dataset currently looks like this: 我的数据集当前如下所示:

  ID Value      Date
1  1     a  1/1/2019
2  1     b  2/5/2018
3  1     a  3/3/2019
4  2     a 6/12/1975
5  2     b  5/4/2017
6  2     b 12/3/2016
7  3     c  1/3/2015
8  3     a  2/1/2015
9  4     a  1/1/1991

and I would like to add another column so that it looks like this: 我想添加另一列,使其看起来像这样:

  ID    Value   Date    Occurence
1  1     a  1/1/2019         1
2  1     b  2/5/2018         1
3  1     a  3/3/2019         2
4  2     a 6/12/1975         1
5  2     b  5/4/2017         2
6  2     b 12/3/2016         1
7  3     c  1/3/2015         1
8  3     a  2/1/2015         1
9  4     a  1/1/1991         1

So we can see, where ID is equal to 1 , a occurs twice and is ranked in order from oldest to newest and b is only ranked once because it only occurs once for ID 1 . 因此,我们可以看到,在ID等于1a出现两次,并按从最旧到最新的顺序进行排序,而b仅排名一次,因为它对于ID 1仅出现一次。

I have come across code to rank things in order of occurrence for the whole data set, but I would like to rank things in order of occurrence within IDs. 我遇到了一些代码,它们按照整个数据集的出现顺序对事物进行排序,但是我想按照ID中的出现顺序对事物进行排名。 There is also code to help me rank all dates within an ID...but I want to be able to look at individual values. 还有代码可以帮助我对ID中的所有日期进行排名...但是我希望能够查看各个值。

How would I do this? 我该怎么做? Thank you! 谢谢!

For the updated question with sorting by date: 对于按日期排序的更新问题:

library(lubridate)
df %>% 
mutate_at("Date",dmy) %>%  #convert to date-object
arrange(Date) %>%  #sort by date
group_by(ID,Value) %>%  #group by ID-Value pairs
mutate(Occurence = row_number()) #mutate new column with occurence

df
     ID Value Date       Occurence
  <int> <chr> <date>         <int>
1     2 a     1975-12-06         1
2     4 a     1991-01-01         1
3     3 a     2015-01-02         1
4     3 c     2015-03-01         1
5     2 b     2016-03-12         1
6     2 b     2017-04-05         2
7     1 b     2018-05-02         1
8     1 a     2019-01-01         1
9     1 a     2019-03-03         2

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

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