title: What is the most efficient way to select a set of variable names from an R data.frame?
body: <h3>The problem:</h3>
  <p>I often need to select a set of variables from a data.frame in R.
  My research is in the social and behavioural sciences, and it is quite common to have a data.frame with several hundreds of variables (e.g., there'll be item level information for a range of survey questions, demographic items, performance measures, etc., etc.).</p>
  <p>As part of analyses, I'll often want to select a subset of variables.
  For example, I might want to get:</p>
  <ul>
  <li>descriptive statistics for a set of variables</li>
  <li>correlation matrix on a set of variables</li>
  <li>factor analysis on a set of variables</li>
  <li>predictors in a linear model</li>
  </ul>
  <p>Now, I know that there are many ways to write the code to select a subset of variables.
  <a href="http://www.statmethods.net/management/subset.html" rel="noreferrer">Quick-r has a nice overview of common ways of extracting variable subsets from a data.frame</a>.</p>
  <p>e.g.,</p>
  <pre><code>myvars &lt;- c(&quot;v1&quot;, &quot;v2&quot;, &quot;v3&quot;)
  newdata &lt;- mydata[myvars]
  </code></pre>
  <p>However, I'm interested in the efficiency of this process, particularly where you might need to extract 20 or so variables from a data.frame. The naming convention of variables is often not intuitive, especially where you've inherited a dataset from someone else, so you might be left wondering, was the variable <code>Gender</code>, <code>gender</code>, <code>sex</code>, <code>GENDER</code>, <code>gender1</code>, etc.
  Multiply this by 20 variables that need to be extracted, and the task of memorising variable names becomes more complicated than it needs to be.</p>
  <h3>Concrete example</h3>
  <p>To make the following discussion concrete, I'll use the <code>bfi</code> data.frame in the <code>psych</code> package.</p>
  <pre><code>library(psych)
  data(bfi)
  df &lt;- bfi
  head(df, 1)
        A1 A2 A3 A4 A5 C1 C2 C3 C4 C5 E1 E2 E3 E4 E5 N1 N2 N3 N4 N5 O1 O2 O3 O4
  61617  2  4  3  4  4  2  3  3  4  4  3  3  3  4  4  3  4  2  2  3  3  6  3  4
        O5 gender education age
  61617  3      1        NA  16
  </code></pre>
  <ul>
  <li>How can I efficiently select an arbitrary set of variables, which for concreteness, I'll choose <code>A1, A2, A3, A5, C2, C3, C5, E2, E3, gender, education, age</code>?</li>
  </ul>
  <h3>My current strategy</h3>
  <p>I currently have a range of strategies that I use.
  Of course sometimes I can exploit things like the numeric position of the variables or the naming convention and use either <code>grep</code> to select or <code>paste</code> to construct. But sometimes I need a more general solution. I've used the following over the while:</p>
  <h3>1. names(df)</h3>
  <p>In the early days, I used to call <code>names(df)</code>, copy the quoted variable names and then edit until I have what I want.</p>
  <h3>2. Use a database</h3>
  <p>Sometimes I'll have a separate data.frame that stores each variable as a row, and has columns for variable names, variable labels, and it has a column which indicates whether the variable should be retained for a particular analysis. I can then filter on that <code>include</code> variable and extract a vector of variable names. I find this particularly useful when I'm developing a psychological test and for various iterations I want to include or exclude certain items.</p>
  <h3>3. dput(names(df))</h3>
  <p>As <a href="http://jeromyanglim.blogspot.com.au/2009/10/efficient-variable-selection-in-r.html" rel="noreferrer">Hadley Wickham once pointed out to me</a> <code>dput</code> is a good option; e.g., <code>dput(names(df))</code> is better than <code>names(df)</code> in that it outputs a list that is already in the <code>c(&quot;var1&quot;, &quot;var2&quot;, ...)</code> format:</p>
  <pre><code>dput(names(df))
  c(&quot;A1&quot;, &quot;A2&quot;, &quot;A3&quot;, &quot;A4&quot;, &quot;A5&quot;, &quot;C1&quot;, &quot;C2&quot;, &quot;C3&quot;, &quot;C4&quot;, &quot;C5&quot;, 
  &quot;E1&quot;, &quot;E2&quot;, &quot;E3&quot;, &quot;E4&quot;, &quot;E5&quot;, &quot;N1&quot;, &quot;N2&quot;, &quot;N3&quot;, &quot;N4&quot;, &quot;N5&quot;, &quot;O1&quot;, 
  &quot;O2&quot;, &quot;O3&quot;, &quot;O4&quot;, &quot;O5&quot;, &quot;gender&quot;, &quot;education&quot;, &quot;age&quot;)
  </code></pre>
  <p>This can then be copied into the script and edited.</p>
  <h3>But can it be more efficient</h3>
  <p>I guess <code>dput</code> is a pretty good variable selection strategy. The efficiency of the process largely depends on how proficient you are in copying the text into your script and then editing the list of names down to those desired.</p>
  <p>However, I still remember the efficiency of GUI based systems of variable selection.
  For example, in SPSS when you interact with a dialogue box you can point and click with the mouse the variables you want from the dataset. You can shift-click to select a range of variables, you can hold shift and press the down key to select one or more variables, and so on. And then you can press <code>Paste</code> and the command with extracted variable names is pasted into your script editor.</p>
  <h3>So, finally the core question</h3>
  <ul>
  <li>Is there a simple no frills GUI device that permits the selection of variables from a data.frame (e.g., something like <code>guiselect(df)</code> opens a gui window for variable selection), and returns a vector of variable names selected <code>c(&quot;var1&quot;, &quot;var2&quot;, ...)</code>?</li>
  <li>Is <code>dput</code> the best general option for selecting a set of variable names in R? Or is there a better way?</li>
  </ul>
  <p><strong>Update (April 2017):</strong> I have posted my own understanding of a <a href="https://stackoverflow.com/a/43244314/180892">good strategy below</a>.</p>