随着板球世界杯的进行,我想创建一个我自己的Duckworth-Lewis计算器,用于一天的国际人士,使用R。
这是我分配自己的挑战,要推动自己更多地了解R以及我能做什么。达克沃思·刘易斯(Duckworth-Lewis(是板球中使用的算法,当时意外延误(尤其是恶劣的天气(是中心舞台。该算法(一日国际(涉及计算团队2的PAR得分,这是"团队2"目标"等于"团队1得分"乘以"团队2资源"one_answers" Team 1 Resources"的商的位置,我们将1添加到1个。找到目标(否则它为南非2003年世界杯场景创造了空间(。
team2_target = function(team1_score, team1_resources, team2_resources) {
return((team1_score * (team2_resources/team1_resources) + 1)
}
我想让自己的功能使用丢失的小门的数量,以及剩下的剩余的距离来计算"团队2资源"变量。例如,如果一支球队在整个50分中获得277分,而车队2则得分为240,而40分后的4个小门损失了4个小门,我希望能够将" Overs"one_answers"检票口丢失"用作变量。这听起来真的很简单,但是这两个因素都很重要,如果我所需的变量中的任何一个都会发生变化,team2_resources变量本身都会改变。
这是做您需要的一种方法。
首先,我使用函数从您的Excel表中查找Team 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