R不断显示错误:
Error in [.default(P, a, ) : incorrect number of dimensions
我想做的是使用为团队的本垒打进攻和防守实力创建的值,然后使用这些值创建概率表来查找两支球队之间的预期目标。
我的代码:
ProbTable <- function(Attack_Def_strength,Home.Team,Away.Team)
{
teams <- rownames(Attack_Def_strength$Club)
P <- Attack_Def_strength$Club
a <- which(teams == Home.Team)
b <- which(teams == Away.Team)
x <- exp(P[a,]$'Home Attacking Strength' - P[b,]$'Home Defending Strength')
y <- exp(P[b,]$'Away Atacking Strength' - P[a,]$'Away Defending Strength')
A <- as.numeric()
B <- as.numeric()
for(i in 0:7) {
A[(i+1)] <- dpois(i,x)
B[(i+1)] <- dpois(i,y)
}
A[8] <- 1 - sum(A[1:8])
B[8] <- 1 - sum(B[1:8])
name <- c("0","1","2","3","4","5","6","7+")
zero <- mat.or.vec(8,1)
C <- data.frame(row.names=name)
for(j in 1:8) {
for(k in 1:8) {
C[j,k] <- A[k]*B[j]
}
}
colnames(C) <- name
return(round(C*100,2))
}
Probabilities <- ProbTable(Attack_Def_strength,"Arsenal","QPR")
这是回溯显示的内容:
Error in `[.default`(P, a, ) : incorrect number of dimensions
4.
NextMethod("[")
3.
`[.factor`(P, a, )
2.
P[a, ]
1.
ProbTable(Attack_Def_strength, "Arsenal", "QPR")