Duckworth-Lewis-Stern 计算器计算修改后的目标
Duckworth-Lewis-Stern calculator to calculate the revised target
随着板球世界杯的进行,我想使用 R 创建一个属于我自己的 Duckworth-Lewis 计算器,用于一日国际比赛。
这是我给自己分配的一个挑战,目的是促使自己更多地了解 R 以及我可以做什么。 Duckworth-Lewis 是一种在意外延误(尤其是恶劣天气)占据中心位置时用于板球比赛的算法。该算法(在一日国际赛中)涉及计算团队 2 的标准杆分数,其中 'Team 2 Target' 等于 'Team 1 Score' 乘以 'Team 2 Resources' 和 'Team 1 Resources' 的商,然后我们将 1 添加到找到目标(否则它会为 2003 年南非世界杯场景创造空间)。
team2_target = function(team1_score, team1_resources, team2_resources) {
return((team1_score * (team2_resources/team1_resources) + 1)
}
我想让我的函数使用丢失的小门数以及剩余的球数来计算 'Team 2 Resources' 变量。例如,如果第 1 队在其完整的 50 轮中得分 277,而第 2 队得分 240,在 40 轮后失去 4 个小门,我希望能够使用 'Overs' 和 'Wickets Lost' 作为变量。这听起来很简单,但这两个因素都很重要,如果我想要的变量中的任何一个发生变化,team2_resources 变量本身也会发生变化。
这里有一种方法可以满足您的需求。
首先,我使用一个函数从您的 excel table 中查找团队 2 的资源(您需要将文件路径更改为您存储 DuckworthLewisStern.xlsx
的任何位置).我使用 dplyr
函数来查找。请参阅 for more information and this 了解 rlang
包中的 quosures 更新。
然后我获取该函数的输出并将其输入到您的 team2_target
函数中,以获取 1 个三柱门丢失和 37 个失球的示例情况的目标值。
library(dplyr)
DLdf <- readxl::read_xlsx("~/Downloads/DuckworthLewisStern.xlsx")
head(DLdf)
# A tibble: 6 x 11
`Wickets Lost` `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Overs Left NA NA NA NA NA NA NA NA NA NA
2 50 1 0.934 0.851 0.749 0.627 0.49 0.349 0.22 0.119 0.047
3 49 0.991 0.926 0.845 0.744 0.625 0.489 0.349 0.22 0.119 0.047
4 48 0.981 0.917 0.838 0.74 0.622 0.488 0.349 0.22 0.119 0.047
5 47 0.971 0.909 0.832 0.735 0.619 0.486 0.349 0.22 0.119 0.047
6 46 0.961 0.9 0.825 0.73 0.616 0.485 0.348 0.22 0.119 0.047
# a function to look up team 2's resources
get_team2_resources <- function(wickets_lost, overs_left) {
# convert the input arguments so we can use them in the filtering and selecting below
wl <- as_label(enquo(wickets_lost))
ol <- as.character(overs_left)
# do the filtering to get the value we want
DLdf %>%
filter(`Wickets Lost` == ol) %>%
select(wl) %>%
pull()
}
# your existing team2_target function
team2_target = function(team1_score, team2_resources) {
return((team1_score * team2_resources) + 1)
}
# EXAMPLE: what are the team 2 resources when 1 wicket is lost and 37 overs are left?
t2res <- get_team2_resources(wickets_lost = 1, overs_left = 37)
t2res
[1] 0.809
# what is the team 2 target when team 1 have scored 100?
team2_target(team1_score = 100, team2_resources = t2res)
[1] 81.9
随着板球世界杯的进行,我想使用 R 创建一个属于我自己的 Duckworth-Lewis 计算器,用于一日国际比赛。
这是我给自己分配的一个挑战,目的是促使自己更多地了解 R 以及我可以做什么。 Duckworth-Lewis 是一种在意外延误(尤其是恶劣天气)占据中心位置时用于板球比赛的算法。该算法(在一日国际赛中)涉及计算团队 2 的标准杆分数,其中 'Team 2 Target' 等于 'Team 1 Score' 乘以 'Team 2 Resources' 和 'Team 1 Resources' 的商,然后我们将 1 添加到找到目标(否则它会为 2003 年南非世界杯场景创造空间)。
team2_target = function(team1_score, team1_resources, team2_resources) {
return((team1_score * (team2_resources/team1_resources) + 1)
}
我想让我的函数使用丢失的小门数以及剩余的球数来计算 'Team 2 Resources' 变量。例如,如果第 1 队在其完整的 50 轮中得分 277,而第 2 队得分 240,在 40 轮后失去 4 个小门,我希望能够使用 'Overs' 和 'Wickets Lost' 作为变量。这听起来很简单,但这两个因素都很重要,如果我想要的变量中的任何一个发生变化,team2_resources 变量本身也会发生变化。
这里有一种方法可以满足您的需求。
首先,我使用一个函数从您的 excel table 中查找团队 2 的资源(您需要将文件路径更改为您存储 DuckworthLewisStern.xlsx
的任何位置).我使用 dplyr
函数来查找。请参阅 rlang
包中的 quosures 更新。
然后我获取该函数的输出并将其输入到您的 team2_target
函数中,以获取 1 个三柱门丢失和 37 个失球的示例情况的目标值。
library(dplyr)
DLdf <- readxl::read_xlsx("~/Downloads/DuckworthLewisStern.xlsx")
head(DLdf)
# A tibble: 6 x 11
`Wickets Lost` `0` `1` `2` `3` `4` `5` `6` `7` `8` `9`
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Overs Left NA NA NA NA NA NA NA NA NA NA
2 50 1 0.934 0.851 0.749 0.627 0.49 0.349 0.22 0.119 0.047
3 49 0.991 0.926 0.845 0.744 0.625 0.489 0.349 0.22 0.119 0.047
4 48 0.981 0.917 0.838 0.74 0.622 0.488 0.349 0.22 0.119 0.047
5 47 0.971 0.909 0.832 0.735 0.619 0.486 0.349 0.22 0.119 0.047
6 46 0.961 0.9 0.825 0.73 0.616 0.485 0.348 0.22 0.119 0.047
# a function to look up team 2's resources
get_team2_resources <- function(wickets_lost, overs_left) {
# convert the input arguments so we can use them in the filtering and selecting below
wl <- as_label(enquo(wickets_lost))
ol <- as.character(overs_left)
# do the filtering to get the value we want
DLdf %>%
filter(`Wickets Lost` == ol) %>%
select(wl) %>%
pull()
}
# your existing team2_target function
team2_target = function(team1_score, team2_resources) {
return((team1_score * team2_resources) + 1)
}
# EXAMPLE: what are the team 2 resources when 1 wicket is lost and 37 overs are left?
t2res <- get_team2_resources(wickets_lost = 1, overs_left = 37)
t2res
[1] 0.809
# what is the team 2 target when team 1 have scored 100?
team2_target(team1_score = 100, team2_resources = t2res)
[1] 81.9