title: Nested subsetting with "["
body: <p>I recently discovered that, after subsetting an object (i.e. a data frame) with "[", the resulting object could be subset with "[" on the same line of code (I should have realized it earlier!). Here is an example:</p>
  
  <pre><code># Create a data frame
  df1 &lt;- as.data.frame(matrix(1:9, nrow = 3))
  
  # Take a look at the data frame
  df1
    V1 V2 V3
  1  1  4  7
  2  2  5  8
  3  3  6  9
  
  # If I want the value which is on the 3rd row and 2nd column
  df1[3,2]
  [1] 6
  
  # But I could also
  df1[,2][3]
  [1] 6
  </code></pre>
  
  <p>A few words on the second alternative. <code>df[,2]</code> returns an atomic vector, which is then subset with <code>df[,2][3]</code>.</p>
  
  <p>The following data frame will be helpful to illustrate my issue. It is a simple data frame containing the name of 26 students, their respective department as well as a numeric value. A seed number is added for reproducibility.</p>
  
  <pre><code>set.seed(123)
  df2 &lt;- data.frame(name = letters, dept = sample(c("econ", "stat", "math"), 26, replace = TRUE), value = runif(26, 0, 100))
  head(df2)
    name dept    value
  1    a econ 54.40660
  2    b math 59.41420
  3    c stat 28.91597
  4    d math 14.71136
  5    e math 96.30242
  6    f econ 90.22990
  </code></pre>
  
  <p><strong><em>I would like to know who has the lowest value in the <code>econ</code> department.</em></strong> The first thing I tried was:</p>
  
  <pre><code>df2[df2$dept == "econ" &amp; df2$value == min(df2$value),]
  [1] name  dept  value
  &lt;0 rows&gt; (or 0-length row.names)
  </code></pre>
  
  <p>It took me a while to understand what I was doing wrong, but I finally realized that the problem was that my code assumed that the person who had the lowest value overall was also from the <code>econ</code> department, which is not the case (and that's the answer that <code>R</code> gave me). Actually, the person with the lowest value overall is from the <code>stat</code> department.</p>
  
  <pre><code>i &lt;- which(df$value == min(df$value))
  df[i,]
    name dept    value
  9    i stat 2.461368
  </code></pre>
  
  <p>Of course, I can easily find the answer to my question with:</p>
  
  <pre><code>df_econ &lt;- df2[df2$dept == "econ",]
  df_econ
     name dept    value
  1     a econ 54.40660
  6     f econ 90.22990
  15    o econ 14.28000
  17    q econ 41.37243
  18    r econ 36.88455
  19    s econ 15.24447
  df_econ[df_econ$value == min(df_econ$value),]
     name dept value
  15    o econ 14.28
  </code></pre>
  
  <p>But I would like to know if I can get the same result using "nested" subsetting with the <code>[</code> operator. What I mean is with a code like this:</p>
  
  <pre><code>df2[df2$dept == "econ",][... ,]
  </code></pre>
  
  <p>I do not know how to refer to the <code>value</code> column at this point since the resulting data frame of the first subsetting operation <code>df2[df2$dept == "econ",]</code> is a data frame different from <code>df2</code>. I also know that the <code>value</code> column is the 3rd column, but I do not know how to set subsetting conditions using column indexes rather than their names.</p>
  
  <p>Thank you for your help.</p>