我有一个有效的自定义函数,但不确定如何让它与输入列表循环。看起来我需要了解 apply() 等,但我对当前的设置还不太了解。该函数使用 rollapply() 查找给定时间范围内的最大指标。
library(zoo)
library(dplyr)
# Data
set.seed(1)
df <- tibble(player = rep(LETTERS[1:2], each = 10),
minute = rep(1:10, times = 2),
tdc = sample(100:200,size = 20),
sumad = sample(1:10, size = 20, replace = TRUE))
# Custom function
x_min_roll <- function(df, metric, n_minutes, fun){
metric <- ensym(metric)
newname <- glue::glue("{rlang::as_string(metric)}_x{as.character(n_minutes)}")
df %>%
# dynamically create new column name based on input
mutate("{newname}" := rollapply(!!metric, n_minutes, fun, align='left', fill=NA)) %>%
group_by(player) %>%
slice_max(.data[[newname]]) %>%
select(player, .data[[newname]])
}
# This works
df %>%
x_min_roll(metric = tdc, n_minutes = 2, fun = sum)
# A tibble: 2 x 2
# Groups: player [2]
player tdc_x2
<chr> <int>
1 A 339
2 B 380
我希望能够做到这一点:
metric_list <- c('tdc', 'sumad')
minutes_list <- c(2,5)
df %>%
x_min_roll(metric = metric_list, n_minutes = minutes_list, fun = sum) %>%
# maybe a few more steps here.... to get this
# A tibble: 2 x 5
player tdc_x2 tdc_x5 sumad_x2 sumad_x5
<chr> <dbl> <dbl> <dbl> <dbl>
1 A 339 793 20 36
2 B 380 866 19 41