1 1 2 2 3 3 4 4
我希望在组中创建一个标志。分组在Round上,然后在Team上,如下所示。每轮得分最低的球队的旗帜应为0,最大得分为1,如果每轮得分相同,则为2。列"的预期输出;标志";样本数据如下:
团队得分df %>%
group_by(Round) %>%
# mutate(Flag = case_when(Score == max(Score) & Score == min(Score) ~ 2, # equivalent
mutate(Flag = case_when(diff(range(Score)) == 0 ~ 2,
Score == max(Score) ~ 1,
TRUE ~ 0))
df %>%
group_by(Round) %>%
mutate( Flag1 = replace(rank(Score) - 1, length(unique(Score)) == 1, 2))
Round Team Score Flag Flag1
<int> <chr> <int> <int> <dbl>
1 1 Team1 4 0 0
2 1 Team2 8 1 1
3 2 Team1 9 1 1
4 2 Team2 2 0 0
5 3 Team1 6 2 2
6 3 Team2 6 2 2
7 4 Team1 14 1 1
8 4 Team2 9 0 0