如何在对 ID 分组时将 R 中的非顺序数据转换为顺序数据

Eri*_*Tim 1 r dplyr

我有以下数据框。

df <- data.frame(Person = c("Eric","Eric","Eric","Joe","Joe","Joe"), Order = c(2,7,4,2,5,1),
                 Value = c("A","A","B","C","A","B"))
Run Code Online (Sandbox Code Playgroud)

订单列当前处于随机顺序。每个人都有 3 个订单值,它们是 1 到 8 之间的随机整数。订单始终是 1 到 8 之间的值,并且没有重复。如何转换 Order 列,使其反映按人分组的值的顺序?因此,订单列将始终介于 1 和 3 之间。所需的输出将如下所示。

df <- data.frame(Person = c("Eric","Eric","Eric","Joe","Joe","Joe"), Order = c(1,3,2,2,3,1),
                 Value = c("A","A",'B","C","A","B"))```
Run Code Online (Sandbox Code Playgroud)

akr*_*run 5

也许,我们需要rank按“人”分组的“订单”

library(dplyr)
df %>% 
   group_by(Person) %>%
   mutate(Order = rank(Order))
Run Code Online (Sandbox Code Playgroud)