title: Error in for loop in ELO rating algorithm
body: <p>I'm trying to run the ELO rating code from here</p>
  <p><a href="https://github.com/xsankar/hairy-octo-hipster/blob/master/ELO-538.R" rel="nofollow noreferrer">https://github.com/xsankar/hairy-octo-hipster/blob/master/ELO-538.R</a></p>
  <p>The dataset I'm using is here</p>
  <p><a href="https://www.mediafire.com/file/0a5v393ki775vw9/sport2.xlsx/file" rel="nofollow noreferrer">https://www.mediafire.com/file/0a5v393ki775vw9/sport2.xlsx/file</a></p>
  <p>The code I'm running is slightly modificated, I'm working with only 17 weeks instead of 22 as in the original code, but I could not make it work either with 22 weeks</p>
  <pre><code>##########################################################
  
  library(dplyr)
  
  data&lt;- read_xlsx(&quot;C:/User/sport2.xlsx&quot;)
   
  data$PtsW &lt;- as.numeric(data$PtsW)
  data$PtsL &lt;- as.numeric(data$PtsL)
  data$YdsW &lt;- as.numeric(data$YdsW)
  data$YdsL &lt;- as.numeric(data$YdsL)
  data$TOW &lt;- as.numeric(data$TOW)
  data$TOL &lt;- as.numeric(data$TOL)
  
  rankings &lt;- data_frame()
  teams &lt;- data %&gt;% distinct(Winner.tie) %&gt;% select(Winner.tie)
  teams
  rankings &lt;- bind_rows(rankings,teams)
  #print(data)
  #class(rankings)
  #print(rankings)
  rankings
  
  for (i in 2:19) {
    rankings[,i] &lt;- 0
  }
  colnames(rankings) &lt;- c(&quot;Team&quot;,&quot;Week.0&quot;,&quot;Week.1&quot;,&quot;Week.2&quot;,&quot;Week.3&quot;,&quot;Week.4&quot;,&quot;Week.5&quot;,&quot;Week.6&quot;,
                          &quot;Week.7&quot;,&quot;Week.8&quot;,&quot;Week.9&quot;,&quot;Week.10&quot;,&quot;Week.11&quot;,&quot;Week.12&quot;,&quot;Week.13&quot;,
                          &quot;Week.14&quot;,&quot;Week.15&quot;,&quot;Week.16&quot;,&quot;Week.17&quot;)
  rankings$Week.0 &lt;- 1500
  
  # Iterate for each week of play
  week.no &lt;- 1
  k_factor &lt;- 20.0
  week.data &lt;- data[data$Week == week.no,]
  
  
  for (j in 1:17) {
    week.no &lt;- j
    k_factor &lt;- 20.0
    week.data &lt;- data[data$Week == week.no,]
    for (i in 1:nrow(week.data)) {
      winner &lt;- week.data[i,&quot;Winner.tie&quot;]
   
      loser &lt;- week.data[i,&quot;Loser.tie&quot;]
      old.rank.w &lt;- rankings[rankings$Team == winner,week.no+1]
      old.rank.w &lt;- old.rank.w[[1]]
      old.rank.l &lt;- rankings[rankings$Team == loser,week.no+1]
      old.rank.l &lt;- old.rank.l[[1]]
      
      # Calculate Margin of Victory Multiplier 
      # mv_mult = LN(ABS(PD)+1) * (2.2/((ELOW-ELOL)*.001+2.2))
      pd &lt;- week.data$PtsW[i] - week.data$PtsL[i]
      mv_mult &lt;- 1 #Margin For Victory Multiplier
      mv_mult &lt;- log(pd +1) * (2.2/((old.rank.w - old.rank.l)*.001+2.2))
      #
      
      # Use old ELO Algorithm
      #
      w_w &lt;- 1.0
      w_l &lt;- 0.0
      if (pd == 0) {
        w_w &lt;- 0.5
        w_l &lt;- 0.5
      }
      #
      
      #
      d_ij_w &lt;- old.rank.w - old.rank.l
      d_ij_l &lt;- old.rank.l - old.rank.w
      #
      mu_ij_w &lt;- 1 / (1 + 10 ^ ((-1 * d_ij_w)/400))
      new.rank.w &lt;- round( old.rank.w + (k_factor * mv_mult * (w_w - mu_ij_w)))
      #
      mu_ij_l &lt;- 1 / (1 + 10 ^ ((-1 * d_ij_l)/400))
      new.rank.l &lt;- round( old.rank.l + (k_factor * mv_mult * (w_l - mu_ij_l)))
      #
      print (sprintf(&quot;Rank : W = %d L = %d&quot;,new.rank.w,new.rank.l))
      rankings[rankings$Team == winner,week.no+2] &lt;- new.rank.w
      rankings[rankings$Team == loser,week.no+2] &lt;- new.rank.l
    } 
    
  ###################################################################3  
    # if team didn't play, carry forward early ratings
    # not needed for wildcard, division et al
  #  for (i in 1:nrow(rankings)) {
  #    if (is.na(rankings[i,week.no+2])) {
  #      rankings[i,week.no+2] &lt;- rankings[i,week.no+1]
  #    }
  #    if (rankings[i,week.no+2] &lt; 1) {
  #      rankings[i,week.no+2] &lt;- rankings[i,week.no+1]
  #    }
  #    }
  ##################################################################
  
  }
  # week #1 ranking
  rankings %&gt;% select(Team,Week.1) %&gt;% arrange(-Week.1)
  # week #17 ranking
  rankings %&gt;% select(Team,Week.17) %&gt;% arrange(-Week.17)
  #  
  </code></pre>
  <p>I'm getting the error</p>
  <pre><code>Error in x[[jj]][iseq] &lt;- vjj : replacement has length zero
  </code></pre>
  <p>I've tried modifying the for indexes and debugging but couldn't find what's wrong</p>