title: How do I extract a single column from a data.frame as a data.frame?
body: <p>Say I have a data.frame:</p>
  
  <pre><code>df &lt;- data.frame(A=c(10,20,30),B=c(11,22,33), C=c(111,222,333))
    A  B  C
  1 10 11 111
  2 20 22 222
  3 30 33 333
  </code></pre>
  
  <p>If I select two (or more) columns I get a data.frame:</p>
  
  <pre><code>x &lt;- df[,1:2]
     A  B
   1 10 11
   2 20 22
   3 30 33
  </code></pre>
  
  <p>This is what I want. However, if I select only one column I get a numeric vector:</p>
  
  <pre><code>x &lt;- df[,1]
  [1] 1 2 3
  </code></pre>
  
  <p>I have tried to use as.data.frame(), which does not change the results for two or more columns. it does return a data.frame in the case of one column, but does not retain the column name:</p>
  
  <pre><code>x &lt;- as.data.frame(df[,1])
       df[, 1]
  1       1
  2       2
  3       3
  </code></pre>
  
  <p>I don't understand why it behaves like this. In my mind it should not make a difference if I extract one or two or ten columns. IT should either always return a vector (or matrix) or always return a data.frame (with the correct names). what am I missing? thanks!</p>
  
  <p>Note: This is not a duplicate of the question about matrices, as matrix and data.frame are fundamentally different data types in R, and can work differently with dplyr. There are several answers that work with data.frame but not matrix. </p>