复杂如果陈述

时间:2014-04-03 12:53:18

标签: r loops if-statement

我需要为我在R中做的循环写一个长if语句来模拟一个赛季的足球比赛。目前我正在使用此代码来模拟匹配结果

mod3=glm(formula = Score ~ as.factor(Attack) + as.factor(Defence) +as.factor(Home)+Division, family = poisson,    data = football)
for (i in 1:92){
 for (j in 1:92){
  if (i!=j){
  teamHome=levels(football$Attack)[i] 
  teamAway=levels(football$Attack)[j]
  homeScore=rpois(1,predict.glm(mod3, data.frame(Attack=teamHome,Defence=teamAway,Home="Y   "),type="response"))
  awayScore=rpois(1,predict.glm(mod3, data.frame(Attack=teamAway,Defence=teamHome,Home="N   "),type="response"))
  Result= if(homeScore>awayScore){
   Result="H"
} else if(homeScore<awayScore){
Result="A"
}else if(homeScore==awayScore){
Result="D"
}


 results<-print(paste(teamHome,homeScore,"      ",teamAway,awayScore,Result),quote=F)
 }
}
}  

目前我的泊松回归的攻击,防御和家庭得分有助于模拟结果。但是我需要添加另一个因素&#34; Division&#34;如果上半部(46)队中的任何一支球队被模拟对阵,那么#34; teamHome&#34;和&#34; teamAway&#34;我返回了一个&#34; Division&#34; &#34; S&#34;的反应如果最底层的46支队伍中的任何一支队伍都是相互模拟的,我会返回&#34;分区&#34;回应&#34; S1&#34;。我将如何在我的循环中加入这个我的关于teamHome和teamAway的级别以供参考

 [1] "Manchester United     " "Manchester City       " "Chelsea               "
 [4] "Arsenal               " "Tottenham             " "Everton               "
  [7] "Liverpool             " "WBA                   " "Swansea               "
 [10] "West Ham              " "Norwich               " "Fulham                "
 [13] "Stoke                 " "Southampton           " "Aston Villa           "
 [16] "Newcastle             " "Sunderland            " "Wigan                 "
 [19] "Reading               " "QPR                   " "Cardiff               "
 [22] "Hull                  " "Watford               " "Brighton              "
 [25] "Crystal Palace        " "Leicester             " "Bolton                "
 [28] "Nottingham Forest     " "Charlton              " "Derby                 "
 [31] "Burnley               " "Birmingham            " "Leeds                 "
 [34] "Ipswich               " "Blackpool             " "Middlesbrough         "
 [37] "Blackburn             " "Sheffield Wednesday   " "Huddersfield          "
 [40] "Millwall              " "Barnsley              " "Peterborough          "
 [43] "Wolves                " "Bristol City          " "Doncaster             "
 [46] "Bournemouth           " "Brentford             " "Yeovil                "
 [49] "Sheffield United      " "Swindon               " "Leyton Orient         "
 [52] "Milton Keynes Dons    " "Walsall               " "Crawley               "
 [55] "Tranmere              " "Notts County          " "Crewe                 "
 [58] "Preston               " "Coventry              " "Shrewsbury            "
 [61] "Carlisle              " "Stevenage             " "Oldham                "
 [64] "Colchester            " "Scunthorpe            " "Bury                  "
 [67] "Hartlepool            " "Portsmouth            " "Gillingham            "
 [70] "Rotherham             " "Port Vale             " "Burton                "
 [73] "Cheltenham            " "Northampton           " "Bradford              "
 [76] "Chesterfield          " "Oxford                " "Exeter                "
 [79] "Southend              " "Rochdale              " "Fleetwood             "
 [82] "Bristol Rovers        " "Wycombe               " "Morecambe             "
 [85] "York                  " "Accrington            " "Torquay               "
 [88] "AFC Wimbledon         " "Plymouth              " "Dagenham and Redbridge"
 [91] "Barnet                " "Aldershot             "

如果您觉得可以提供帮助,或者我的解释和需求解释有任何不清楚的地方,请询问我是否迫切需要帮助。

我的数据集形式的示例

Attack                Defence                    Score Home Division 
1 Chelsea                Aston Villa                8 Y    S                  
2 Arsenal                Reading                    4 N    S                 
3 Arsenal                Newcastle                  7 Y    S                
4 Port Vale              Burton                     7 Y    S                  
5 Liverpool              Newcastle                  6 N    S                  
6 Watford                Leeds                      6 N    S                

由于

史蒂夫

1 个答案:

答案 0 :(得分:0)

忽略我对原帖的最终评论 - 我现在从你的例子中看到你在每一行中都有两个分区,连接成一个字符串。因此,您可以将相关的分区内标签提取为:

homeDiv <- substr(football$division[football$Attack==teamHome][1], 1, 1)
awayDiv <- substr(football$division[football$Attack==teamAway][1], 1, 1)
homeScore <- rpois(..., data.frame(..., division = sprintf("%s%s", homeDiv, awayDiv) ...) ...)

所以第一行提取任意的第一个字符(我选择了第一个字符 - 但它们应该都具有相同的第一个字符),其中攻击团队是所选择的主队。同样对于客队也是如此。然后我们将它们连接起来以获得相关的主题:远离分裂对比