title: Can I do a loop/function for this in R? (and how would I do it?)
body: <p>I'm currently using R to backtest some Football/Soccer Odds, using a model to create my own odds.</p>
  
  <p>At the moment it's quite a very long process and I'm curious as if there is a loop/function that I can make to help speed up the process. </p>
  
  <p>This piece of code collects the results over the full season. </p>
  
  <pre><code>library(dplyr)
  library(rvest)
  library(tidyverse)
  options(max.print = 9999)
  Res &lt;- read_html("https://www.betexplorer.com/soccer/england/premier-league/results/?month=all")
  tbls_ls &lt;- Res %&gt;%
    html_nodes("table") %&gt;%
    .[1] %&gt;%
    html_table(fill = TRUE)
  
  Results &lt;- as.data.frame(tbls_ls)
  Results &lt;- Results[,c(1:2)]
  names(Results) &lt;- c("Fixture","Score")
  Results &lt;- tidyr::separate(Results, Fixture, into =c("HomeTeam","AwayTeam"), sep = " - ")
  Results &lt;- tidyr::separate(Results, Score, into = c("FTHG","FTAG"), sep = ":")
  Results &lt;- Results %&gt;% tidyr::drop_na()
  Results &lt;- Results[,c(1:4)]
  write.csv(Results, file = "Results.csv")
  rownames(Results) &lt;- 1:nrow(Results)
  </code></pre>
  
  <p>I'm backtesting odds by game week and for the League I'm testing there are 10 games per game week. This code removes the previous game week and sets up that weeks fixtures as if they were yet to be played. This removes gameweek 29(the last in the this league)</p>
  
  <pre><code>ResultsEdit &lt;- Results #[-(1:10),]
  FixEdit &lt;- ResultsEdit[,c(1:2)]
  
  ResultsEditE &lt;- Results [-(1:10),]
  ResultsEditE %&gt;% tidyr::drop_na()
  write.csv(Results, file="ResultsEditE")
  
  </code></pre>
  
  <p>If I wanted to remove gameweek 29 &amp; 28 and use gameweek 28 as fixtures yet to be played I'd edit to the code to  </p>
  
  <pre><code>ResultsEdit &lt;- Results [-(1:10),]
  ResultsEditE &lt;- Results [-(1:20),]
  </code></pre>
  
  <p>So on and so forth the futher I go back. </p>
  
  <p>This is the Poisson code for predicting Odds</p>
  
  <pre><code>library("vcd")
  
  source("http://www.maths.leeds.ac.uk/~voss/projects/2010-sports/Football.R")
  results0 &lt;- read.csv("ResultsEditE",stringsAsFactors = F) 
  results0$X &lt;- NULL
  countres &lt;- results0$FTHG + results0$FTAG
  tg &lt;- countres
  fretabtg&lt;-table(tg)
  gf &lt;- goodfit(fretabtg, type="poisson", method="ML")
  Table0 &lt;- Table(results0)
  games &lt;- results0
  g &lt;- nrow(games)
  Y &lt;- matrix(0,2*g,1)
  for (i in 1:g) {
    Y[((2*i)-1)] &lt;- games[i,3]
    Y[(2*i)] &lt;- games[i,4]
  }
  
  teams &lt;- sort(unique(c(games[,1], games[,2])), decreasing = FALSE) 
  n &lt;- length(teams) 
  X &lt;- matrix(0,2*g,((2*n)+1))
  for (i in 1:g) { 
    M &lt;- which(teams == games[i,1]) 
    N &lt;- which(teams == games[i,2]) 
    X[((2*i)-1),M] &lt;- 1 
    X[((2*i)-1),N+n] &lt;- -1 
    X[(2*i),N] &lt;- 1 
    X[(2*i),M+n] &lt;- -1 
    X[((2*i)-1),((2*n)+1)] &lt;- 1 
  }
  
  x &lt;- qr(X)
  x$rank
  XX &lt;- X[,-1]
  
  
  
  TeamParameters &lt;- Parameters(results0)
  SimSeason &lt;- Games(TeamParameters)
  SimSeason &lt;- SimSeason %&gt;% tidyr::drop_na()
  
  SimTable &lt;- Table(SimSeason)
  Simulations &lt;- Sim(TeamParameters,3)
  
  Probabilities &lt;- ProbTable(TeamParameters,"", "")
  ResultProbabilities&lt;- ResultProbs(Probabilities)
  
  cat("\nHome Win True Odds:", 100/ResultProbabilities$HomeWin)
  cat("\nDraw True Odds:", 100/ResultProbabilities$Draw)
  cat("\nAway Win True Odds:", 100/ResultProbabilities$AwayWin)
  
  </code></pre>
  
  <p>And this code gives me the odds for the game week that I want. </p>
  
  <pre><code>run_probs &lt;- function(h_team, a_team) {
    Probabilities &lt;- ProbTable(TeamParameters, h_team, a_team)
    ResultProbabilities &lt;- ResultProbs(Probabilities)
  
    cat(paste("\n", h_team, "VS", a_team))
    cat("\nHome Win:", 100/ResultProbabilities$HomeWin)
    cat("\nDraw:", 100/ResultProbabilities$Draw)
    cat("\nAway Win:", 100/ResultProbabilities$AwayWin)  
  
    return(ResultProbabilities)
  }
  
  FixEdit &lt;- head(FixEdit, n=10)
  
  prob_list &lt;- Map(run_probs, FixEdit$HomeTeam,FixEdit$AwayTeam)
  </code></pre>
  
  <p>What I'm desperate to do is reduce the amount of time it takes me to run through a season. 
  Using the code I've provided as an example is it possible to do some sort of loop for this?  </p>
  
  <pre><code>Run the game week 29 removal code, run the poisson code, run the code for giving me the odds for the game week - save the results in a CSV
  Run the game week 28 removal code, run the poisson code, run the code for giving me the odds for the game week - save the results in a CSV
  </code></pre>
  
  <p>etc etc. </p>
  
  <p>Hopefully returning something like this for each game week. </p>
  
  <pre><code>             Home            Away   Home Win      Draw  Away Win
  1       Leicester     Aston Villa   1.209044  9.009009  16.18123
  2         Chelsea         Everton   1.634788   5.09165  5.216484
  3  Manchester Utd Manchester City      3.125  4.199916  2.265006
  4         Arsenal        West Ham   1.786352   4.52284   4.56621
  5         Burnley       Tottenham    3.08642  3.904725  2.379819
  6  Crystal Palace         Watford   2.309469  3.079766  4.128819
  7       Liverpool     Bournemouth   1.160362  10.04016  25.97403
  8   Sheffield Utd         Norwich   1.637465  3.868472  7.639419
  9     Southampton       Newcastle   2.198769  3.687316  3.654971
  10         Wolves        Brighton   1.785714  4.016064  5.230126
  </code></pre>
  
  <p>Sorry for the long post and sorry if I've made no sense. Feel free to lock/remove post if it just seems like gibberish. </p>