title: Scope of Aggregation Functions when nesting apply(within())
body: <p><em><strong>Edited original post to clarify question</strong></em></p>
  <p><strong>Background</strong><br />
  I'm learning R and saw this scenario and don't understand how R handles (what I'll call) implied context transitions. The script I am trying to understand simply iterates through each row of a matrix and prints the index of the column(s) within that row that contain the minimum value of that row. What I don't understand is how R handles the <em>context transition</em> as different functions are applied to the dependent variable <code>x</code>:</p>
  <ol>
  <li><code>x</code> (when defined as an argument to <code>function(x)</code>) is an <em>atomic vector</em> because of the <code>apply()</code> function with a <code>MARGIN = 1</code> argument</li>
  <li>The <code>which()</code> function then iterates over the individual <em>elements</em> within the atomic vector <code>x</code> to see which ones == <code>min(x)</code></li>
  <li><strong>This is the part that truly confuses me:</strong> Despite the fact <code>which()</code> is iterating over <em>elements</em> of atomic vector <code>x</code>, you can call <code>min(x)</code> within the <code>which()</code> function and R somehow switches <code>x</code> to be defined as the entire <em>atomic vector</em> again for calculating the <code>min()</code> across the vector vs. within the scope of a single <em>element</em></li>
  </ol>
  <p><em>Example Data Matrix</em></p>
  <pre><code>a &lt;- matrix (c(5, 2, 7, 1, 2, 8, 4, 5, 6), 3, 3)
  </code></pre>
  <pre><code>         [,1] [,2] [,3]
  [1,]    5    1    4
  [2,]    2    2    5
  [3,]    7    8    6
  </code></pre>
  <p><em>This is the script that returns the column indexes that I am struggling to understand</em></p>
  <pre><code>apply (a, 1, function(x) which(x == min(x)))
  </code></pre>
  <p><strong>My question:</strong><br /></p>
  <p>Within the <code>which()</code> function, why does <code>min(x)</code> return the minimum of the <em>atomic vector</em> (as is desired) and not the minimum within the scope of an individual <em>element</em> within that vector, since <code>which()</code> is iterating over each individual element within the <em>atomic vector</em> <code>x</code>?</p>