title: How to select all variables in a data.frame that are listed in a text file (in R)?
body: <p>My programming problem is that I have a list of strings, which are variable names, in an Ascii format file, such as <code>variables.txt</code>, e.g.</p>
  
  <pre><code>A
  B
  C
  D
  </code></pre>
  
  <p>From a <code>data.frame</code> in <code>R</code> those variables matching the names in <code>variables.txt</code> should be selected. For example:</p>
  
  <pre><code>var = c("A", "B", "C", "D")
  (dat=data.frame(A=1,B=2,C=3,D=4,E=5,F=6))
    A B C D E F
  1 1 2 3 4 5 6
  </code></pre>
  
  <p>Followed by</p>
  
  <pre><code>dat[,var]
    A B C D
  1 1 2 3 4
  </code></pre>
  
  <p>So the problem maybe comes down to reading <code>variables.txt</code> into <code>var</code>. If I give:</p>
  
  <pre><code>var &lt;- read.table("variables.txt")
  </code></pre>
  
  <p>The result is again a <code>data.frame</code> which cannot be used for sub-setting. What is the most efficient way to complete this task?</p>
  
  <p><strong>Edit:</strong> I found out that </p>
  
  <pre><code>var &lt;- as.vector(as.matrix(read.table("variables.txt")))
  </code></pre>
  
  <p>does the trick. But perhaps there are more efficient ways.</p>