我创建了一个公式公式。我的想法是我正在研究棒球统计数据和测验学。我进行了一系列计算,可以帮助我找到一支球队的获胜概率。合并了其他几个公式的最终公式如下:
GameSBWprobabilityPO <- function(HomeTeam, HomePitcher, AwayTeam, AwayPitcher, season, date){
Hometeam <- GameSBWteamprobsum(HomeTeam, HomePitcher, season, date)
Awayteam <- GameSBWteamprobsum(AwayTeam, AwayPitcher, season, date)
TOTprob <- sum(Hometeam, Awayteam)
HTWp <- (Hometeam/TOTprob)
ATWp <- (Awayteam/TOTprob)
return(HTWp)
}
我想知道是否有一种方法可以使它变得简单
<- function(date, HomeTeam)
并使R自动填充所有包含在同一行中的其他变量。这些变量的列名是这样命名的:
HomeTeam = team1
HomePitcher = pitcher1
AwayTeam = team2
AwayPitcher = pitcher2
season = season
date = date
此外,如何编辑输出?目前,它只是根据我的公式返回主队获胜的概率:
[1] 0.5167891
但是我很想弄清楚如何使它成为当前示例的输出中每个团队相对于彼此的概率:
[1] BOS 51.46%, HOU 48.54%
或类似的东西。 谢谢你
答案 0 :(得分:0)
我喜欢将data.table包用于此类事情。我不知道您的“ GameSBWteamprobsum”函数的作用,因此这是一个使用data.table链接的简化解决方案:
library(data.table)
GameSBWteamprobsum <- function(x, ...){2*x}
DT <- data.table(HomeTeam=runif(10,1,10), HomePitcher=runif(10,1,10), AwayTeam=runif(10,1,10), AwayPitcher=runif(10,1,10), season=runif(10,1,10), date=runif(10,1,10))
DT[, "Hometeam" := GameSBWteamprobsum(HomeTeam, HomePitcher, season, date)][
, "AwayTeam" := GameSBWteamprobsum(AwayTeam, AwayPitcher, season, date)][
, "TOTprob" := sum(Hometeam, AwayTeam)][
, "HTWp" := Hometeam/TOTprob][
, "ATWp" := AwayTeam/TOTprob]
DT
您可能想看看链接部分here
编辑---------------------------------------------- ----------------------
当然,您可以根据一列或多列的条件将行或行的子集传递给函数:
library(data.table)
GameSBWteamprobsum <- function(x){print(x)}
DT <- data.table(HomeTeam=LETTERS[1:10], HomePitcher=runif(10,1,10), AwayTeam=runif(10,1,10), AwayPitcher=runif(10,1,10), season=runif(10,1,10), date=runif(10,1,10))
GameSBWteamprobsum(DT[HomeTeam == "A"])