title: Subsetting with an if....else statement in r
body: <p>I am trying to subset a data frame so that if a column name is present I subset but if not I ignore. For the example I will use mtcars data set. What I am trying to accomplish is if there is a column "vs" subset the first 3 columns and vs. This would be a dateframe named "vsdf". </p>
  
  <pre><code>df &lt;- mtcars
  if(colnames(df)=="vs") {
     vsdf &lt;- df[,1,2,3,"vs"]
  } else {
    NULL
  }
  </code></pre>
  
  <p>Any help or guidance would be greatly appreciated. </p>