title: How to loop through all columns and rows in 2 dataframes within existing R function
body: <p>I'm trying to calculate Brier scores for participants who have answered multiple questions in a survey about the likelihood of different events occurring. </p>
  
  <p>I'm using the existing Brierscore function in R and everything works perfectly when I am only interested in a single column (i.e. answers for all participants for 1 question. I cannot work out how to write a loop to cycle through all columns (questions). </p>
  
  <p>I have 2 dataframes:</p>
  
  <ol>
  <li>correct_answers, contains 1 and 0 to indicate whether event happened</li>
  <li>forecast, participants forecasts on whether something will happen on 0-1 scale. </li>
  </ol>
  
  <p>I have tried multiple for loop structures, and using a for loop for each dataframe. The example below seems closest to what I need but is still not correct. I am trying to work out if my syntax is wrong or if I cannot loop using these types of functions.</p>
  
  <p>This works:</p>
  
  <pre><code>Brier_scores_all_questions &lt;- brierscore(correct_answer$Election_Outcome ~ forecast$Election_Outcome, forecast, decomp = FALSE, bounds = c(0,2),
                           reverse = FALSE, wt = NULL)
  </code></pre>
  
  <p>This is one of the loops I have tried:</p>
  
  <pre><code>for(i in 1:ncol) { 
  
  Brier_scores_all_questions &lt;- brierscore(correct_answer$[[i]] ~ forecast$[[i]], forecast, decomp = FALSE, bounds = c(0,2),
                           reverse = FALSE, wt = NULL)
  
  } # close loop
  </code></pre>
  
  <p>Ideally I would like a matrix with the Brier scores for each participant (rows) and question (columns). Below is the error message I get:</p>
  
  <blockquote>
    <p>for(i in 2:ncol) { 
    + 
    + Brier_scores_all_questions &lt;- brierscore(df_ans_base$[[i]] ~ df_base$[[i]], 
    Error: unexpected '[[' in:
    "
    Brier_scores_all_questions &lt;- brierscore(df_ans_base$[["
                             df_base,decomp = FALSE, bounds = c(0,2),
    Error: unexpected ',' in "                         df_base,"
                             reverse = FALSE, wt = NULL)
    Error: unexpected ',' in "                         reverse = FALSE,"</p>
    
    <p>} 
    Error: unexpected '}' in "}</p>
  </blockquote>