1

这是 df 的一个小代表:

Name <- rep(c("Kobe Bryant", "Julius Randle", "Robert Sacre" ,"Lebron James", "Kevin Love"),c(2,2,2,4,5))

Team <- rep(c("Los Angeles Lakers", "Cleveland Cavaliers"),c(6,9))

Date <- as.Date(c("2015-05-14", "2015-05-15", "2015-05-14","2015-05-18","2015-05-14", "2015-05-20", "2015-05-14","2015-05-16","2015-05-21", "2015-05-22", "2015-05-14","2015-05-16","2015-05-17","2015-06-21","2015-06-22"))

dd <- data.frame(Name,Team,Date)

我想为每个团队创建一个新的变量/列,根据日期从最旧到最近的游戏对不同的游戏进行排名。

输出将如下所示:

        Name                Team       Date desired_output
1    Kobe Bryant  Los Angeles Lakers 2015-05-14              1
2    Kobe Bryant  Los Angeles Lakers 2015-05-15              2
3  Julius Randle  Los Angeles Lakers 2015-05-14              1
4  Julius Randle  Los Angeles Lakers 2015-05-18              3
5   Robert Sacre  Los Angeles Lakers 2015-05-14              1
6   Robert Sacre  Los Angeles Lakers 2015-05-20              4
7   Lebron James Cleveland Cavaliers 2015-05-14              1
8   Lebron James Cleveland Cavaliers 2015-05-16              2
9   Lebron James Cleveland Cavaliers 2015-05-21              4
10  Lebron James Cleveland Cavaliers 2015-05-22              5
11    Kevin Love Cleveland Cavaliers 2015-05-14              1
12    Kevin Love Cleveland Cavaliers 2015-05-16              2
13    Kevin Love Cleveland Cavaliers 2015-05-17              3
14    Kevin Love Cleveland Cavaliers 2015-06-21              4
15    Kevin Love Cleveland Cavaliers 2015-06-22              5

我想到了拆分 - 应用 - 与排名功能相结合,但我无法让它发挥作用。

4

1 回答 1

3

我们可以使用dense_rankfromdplyr来做到这一点:

library(dplyr)
dd %>% group_by(Team) %>%
       mutate(rank = dense_rank(Date))

            Name                Team       Date rank
1    Kobe Bryant  Los Angeles Lakers 2015-05-14    1
2    Kobe Bryant  Los Angeles Lakers 2015-05-15    2
3  Julius Randle  Los Angeles Lakers 2015-05-14    1
4  Julius Randle  Los Angeles Lakers 2015-05-18    3
5   Robert Sacre  Los Angeles Lakers 2015-05-14    1
6   Robert Sacre  Los Angeles Lakers 2015-05-20    4
7   Lebron James Cleveland Cavaliers 2015-05-14    1
8   Lebron James Cleveland Cavaliers 2015-05-16    2
9   Lebron James Cleveland Cavaliers 2015-05-21    4
10  Lebron James Cleveland Cavaliers 2015-05-22    5
11    Kevin Love Cleveland Cavaliers 2015-05-14    1
12    Kevin Love Cleveland Cavaliers 2015-05-16    2
13    Kevin Love Cleveland Cavaliers 2015-05-17    3
14    Kevin Love Cleveland Cavaliers 2015-06-21    6
15    Kevin Love Cleveland Cavaliers 2015-06-22    7
于 2015-08-12T02:57:48.073 回答