我有一个问题,我重复了一个总体的样本,并希望从候选池中删除抽样度量中具有最低N值的个人.
举一个更具体的例子,想象一场比赛,所有球队每周都会从一场比赛中获得一分,表现最差的两支球队每周都会被淘汰.
对于下面的示例数据集,我们应该有10个团队在1周后剩余,8个团队在2周后,6个团队在3周后,4个团队在4周后.
Reprex Below当前为第一周生成正确的df_result
,但不会为随后的几周生成正确的df_result
.感谢您能提供的任何建议.
library(tibble)
library(dplyr)
library(purrr)
library(tidyr)
library(stringr)
set.seed(42)
df <- tibble::tibble(
week = rep(1:4, 12) %>% sort(),
team = rep(stringr::str_c("team_", 1:12), 4),
score = runif(48, 0, 100) %>% ceiling()
) %>%
dplyr::arrange(score) %>%
# dplyr::group_by(week) %>%
split(.$week)
df_result <- df %>%
purrr::map2(
.x = ., .y = dplyr::lag(.),
~ dplyr::filter(.data = .x, !(team %in% .y$team)) %>% dplyr::slice_max(
.data = .,
order_by = score,
n = -2,
with_ties = TRUE
))
创建于2023-08-17年第reprex v2.0.2页