title: How do you remove columns from a data.frame?
body: <p>Not so much 'How do you...?' but more 'How do YOU...?'</p>
  
  <p>If you have a file someone gives you with 200 columns, and you want to reduce it to the few ones you need for analysis, how do you go about it? Does one solution offer benefits over another?</p>
  
  <p>Assuming we have a data frame with columns col1, col2 through col200. If you only wanted 1-100 and then 125-135 and 150-200, you could:</p>
  
  <pre><code>dat$col101 &lt;- NULL
  dat$col102 &lt;- NULL # etc
  </code></pre>
  
  <p>or</p>
  
  <pre><code>dat &lt;- dat[,c("col1","col2",...)]
  </code></pre>
  
  <p>or </p>
  
  <pre><code>dat &lt;- dat[,c(1:100,125:135,...)] # shortest probably but I don't like this
  </code></pre>
  
  <p>or</p>
  
  <pre><code>dat &lt;- dat[,!names(dat) %in% c("dat101","dat102",...)]
  </code></pre>
  
  <p>Anything else I'm missing? I know this is sightly subjective but it's one of those nitty gritty things where you might dive in and start doing it one way and fall into a habit when there are far more efficient ways out there. Much like this question about <a href="https://stackoverflow.com/questions/6918657/whats-the-use-of-which/6921992#6921992">which</a>. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Or, is there an easy way to create a workable vector of column names? name(dat) doesn't print them with commas in between, which you need in the code examples above, so if you print out the names in that way you have spaces everywhere and have to manually put in commas... Is there a command that will give you "col1","col2","col3",... as your output so you can easily grab what you want?</p>