title: lists, data.frame loops & indexing questions
body: <p>I have some python and numpy experience but have never used R before. I'm trying to help my wife with her R project since although she has a much better grasp on statistics, she has little programming experience. I'm finding the syntax and documentation of R very confusing.</p>
  
  <p>The original thing We wanted to do was loop through a large data.frame, do a bunch of spacial calculations involving prior and subsequent records, a little trig and some quality checks on the data and generate a new object with the data. We then will get this new data into GIS</p>
  
  <hr>
  
  <p><strong>EDIT:</strong>
  Just to be clear, the calculations in this example are just a placeholder, and are nothing like the actual calculations I needed to do.</p>
  
  <hr>
  
  <p>Initially I tried something like this:</p>
  
  <pre><code>&gt; result = list()
  &gt; for (i in 1:5) {
  +   #Calculate some dummy data. The actual calculations are much more involved
  +   param1 = i * 1.1
  +   param2 = i * 5.3
  +   param3 = i + a_value
  +   # Now append these calculated values to some sort of object
  +   sample = list(param1=param1,param2=param2,param3=param3)
  +   result &lt;- rbind(result,sample)
  + }
  &gt; print(result)
         param1 param2 param3
  sample 1.1    5.3    12    
  sample 2.2    10.6   13    
  sample 3.3    15.9   14    
  sample 4.4    21.2   15    
  sample 5.5    26.5   16
  </code></pre>
  
  <p>The "sample" column seems un-necessary, but oh well, it looks good. Now to reference a single column...</p>
  
  <pre><code>&gt; result$param2
  NULL
  </code></pre>
  
  <p>???I tried getting rid of 'sample' by:</p>
  
  <pre><code>+   result &lt;- rbind(result,list(param1=param1,param2=param2,param3=param3))
  &gt;
       param1 param2 param3
  [1,] 1.1    5.3    12    
  [2,] 2.2    10.6   13    
  [3,] 3.3    15.9   14    
  [4,] 4.4    21.2   15    
  [5,] 5.5    26.5   16 
  &gt; result$param2
  NULL
  </code></pre>
  
  <p>Perhaps this data frame thing will work. I changed the first line to:</p>
  
  <pre><code>result = data.frame()
  &gt;
     param1 param2 param3
  2     1.1    5.3     12
  21    2.2   10.6     13
  3     3.3   15.9     14
  4     4.4   21.2     15
  5     5.5   26.5     16
  &gt; result$param2 # One column
  [1]  5.3 10.6 15.9 21.2 26.5
  &gt; result[2,] #One row
     param1 param2 param3
  21    2.2   10.6     13
  &gt; result[3,]$param3 # Single value
  [1] 14
  </code></pre>
  
  <p>So it's working, but I'm not sure what the 21 (row number?) is all about. If I have more rows, the 21st row is '211'.</p>
  
  <p>Could someone tell me why the first case didn't work, what the '21' is all about, and if there is a better way to do this. Much of what I've read indicates that loops in R are a sign you don't know what you are doing, but the learning curve on the alternatives seems steep. This is also why the script takes an <strong>amazingly</strong> long time to run, even on a fast machine.</p>