title: Loop through irregular list of numbers to append rows to summary table
body: <p>I'm trying to write code that will loop through a list of integers, which relate to a number of sensors, to provide summary statistics (at this stage just cor()).</p>
  
  <pre><code>    # GOOD TO HERE
      corr_table &lt;-data.frame(ID = integer()
                              , HxT = double())
      for(j in gt_thrsh_key){ #this is currently set to 2:5 for testing - its a list of sensors I want to summarise
  
              # extract humidity and time vectors
              x &lt;- sqldf(sprintf("SELECT humidity FROM data_agg_2 WHERE ID = %s",j))
              y &lt;- sqldf(sprintf("SELECT time_elapsed FROM data_agg_2 WHERE ID = %s",j))
  
              # format into row
              new_row &lt;- data.frame(ID = c(j), HxT = c(cor(x,y))) #insert new variables into row
  
  
              # append to dataframe
              corr_table &lt;- rbind(corr_table, new_row)
              print(sprintf("Sensor %s has been summarised.",j)) # check 1
              print(cor(x,y)) # check 2
  
  
  
      }
      print(corr_table)
      assign("data_agg_2", data_agg_2, envir = .GlobalEnv)
  </code></pre>
  
  <p>I get output:</p>
  
  <pre><code>    [1] "Sensor 2 has been summarised." "Sensor 3 has been summarised." "Sensor 4 has been summarised." "Sensor 5 has been summarised."
      humidity -0.08950285        
         ID HxT
      1  2 -0.08950285 #INCORRECT
      2  3 -0.08950285 #INCORRECT
      3  4 -0.08950285 #INCORRECT
      4  5 -0.08950285 #correct
  </code></pre>
  
  <p><strong>This is only the correct measurement for the final iteration of loop (id = 5)</strong>, so somehow I must be overwriting previous entries. Does anyone know why this is happening? Or can you recommend a better way to perform this loop?</p>
  
  <p>Thanks!!</p>
  
  <p>EDIT: check 2 which prints the cor() of x and y through the loop confirms that only the final run of loop is calculating a value. Has anyone seen this before?</p>