提问人:the_special_none 提问时间:3/13/2023 最后编辑:the_special_none 更新时间:3/15/2023 访问量:54
根据 R 中其他两列的有序值和分组值创建变量
create a variable based on ordered and grouped values of two other columns in R
问:
我在 R 中有一个如下表:
团队 | 比赛 | 池 | 年 | 套比 | 积分比 |
---|---|---|---|---|---|
团队 1 | 比赛 1 | 一个 | 2011 | 3.00 | 0.89 |
团队 2 | 比赛 1 | 一个 | 2011 | 1.00 | 1.57 |
团队 3 | 比赛 1 | 一个 | 2011 | 1.50 | 1.11 |
团队 4 | 比赛 1 | b | 2011 | 1.33 | 1.05 |
我想创建一个新列 (final_standing),给出每个池中每支球队的最终排名(基本上是按池、年份和锦标赛分组)。
最终排名遵循两个标准:
- 套比
- 点数比(如果设定比相等)
我尝试过这样的方法:
library(dplyr)
data = data %>%
group_by(pool, year, tournament) %>%
mutate(pool_position = order(sets ratio, points ratio, runif(sets ratio), decreasing = TRUE))
但它并没有给出我所期望的。
他们还向我提出了以下建议:
tmp <- aggregate(list(a = data$sets_ratio, b = data$points_ratio), list(p = data$pool, y = data$year, t = data$tournament), function(x) sort(-x))
m <- match(paste0(data$pool, data$year, data$tournament), paste0(tmp$p, tmp$y, tmp$t))
df[,c(“sets_rk”, “points_rk”)] <- tmp[m,c(4:5)]
但它分别返回集合和点数排名。相比之下,我想要一个唯一的变量,它首先评估组数比率,如果两个团队之间的组数比率相等,则首先评估点数比率。
答:
0赞
dufei
3/15/2023
#1
我冒昧地在您的示例数据中创建了一个领带。 以这样的方式编码,即更高的比率导致更高的 .sets.ratio
final_standing
final_standing
library(tidyverse)
df <- tibble::tribble(
~team, ~tournament, ~pool, ~year, ~sets.ratio, ~points.ratio,
"team 1", "tournament 1", "a", 2011L, 3, 0.89,
"team 3", "tournament 1", "a", 2011L, 1.5, 1.11,
"team 2", "tournament 1", "a", 2011L, 1.5, 1.57,
"team 4", "tournament 1", "b", 2011L, 1.33, 1.05
)
df |>
mutate(final_standing = row_number(pick(sets.ratio, points.ratio)),
.by = c(year, tournament, pool))
#> # A tibble: 4 × 7
#> team tournament pool year sets.ratio points.ratio final_standing
#> <chr> <chr> <chr> <int> <dbl> <dbl> <int>
#> 1 team 1 tournament 1 a 2011 3 0.89 3
#> 2 team 3 tournament 1 a 2011 1.5 1.11 1
#> 3 team 2 tournament 1 a 2011 1.5 1.57 2
#> 4 team 4 tournament 1 b 2011 1.33 1.05 1
创建于 2023-03-14 使用 reprex v2.0.2
评论