title: How to fill in a dataframe with results from a for loop
body: <p>I am trying to write a for loop in R, which calculates the distance between two vectors (the jth row of a matrix [Z], and a vector, [F1]), and puts the distance into the second column of a dataframe called results. The first column of the results is filled in with years, starting with the year 1906. So the for loop would run through this calculation for each row of [Z], and fill in each row of &quot;results&quot; after each iteration.  Here is the code that I have so far- the code does what I would like if I remove the for loop and run each line individually (for example, if I replace all the j's with 1), but when I add in the for loop it does not work. I don't get an error message, but none of the values show up in the results dataframe. If anyone can help me figure out what I'm missing, that would be great!</p>
  <p>Just a side note, Z is a 97 x 22 matrix and F1 is a 1 x 22 matrix.</p>
  <pre><code>   results &lt;- data.frame(matrix(nrow = 97, ncol = 2)) 
     colnames(results) &lt;- c(&quot;year&quot;, &quot;distance&quot;)
     for(j in 1:97) {
      data &lt;- as.numeric(Z[c(j),])
      data2 &lt;- as.numeric(t(F1))
      distance &lt;- euclidian.dist(data, data2)
      results[j, 1] &lt;- j + 1905
      results[j, 2] &lt;- distance
     }
     View(results)
  </code></pre>