title: remove/subset or select columns based on a part of the header name
body: <p>I have a dataframe of 15000 obs. of 45 variables, with a similar structure/mode/class as the example input dataframe given below.</p>
  
  <p>What I would like to do is select the "Date" column + columns containing "_MIN" as part of the header and put them into a new df. &amp; select the "Date" column + columns containing "_MIN" as part of the header and put them into a new df.</p>
  
  <p>So I am basically looking for <strong>a way to select or subset whole columns based on a part of the name of the column header.</strong> An example: <em>multiple columns contain a "_" underscore and I would like to delete or select these columns.</em> </p>
  
  <pre><code>INPUT dataframe:
  Example &lt;- data.frame(Date=seq(as.Date("1979/1/1"),as.Date("1979/1/5"), by="day"), ca_MIN=1:5, ca_MAX=2:6, cb_MIN=3:7, cb_MAX=4:8)
  
  &gt;Example
  
      Date                  ca_MIN     ca_MAX     cb_MIN     cb_MAX
      1 1979-01-01          1          2          3          4
      2 1979-01-02          2          3          4          5
      3 1979-01-03          3          4          5          6
      4 1979-01-04          4          5          6          7
      5 1979-01-05          5          6          7          8
  
  OUTPUT dataframes, selected based on the column names "_MAX", "_MIN":
  Example_MIN &lt;- data.frame(Date=seq(as.Date("1979/1/1"),as.Date("1979/1/5"),     by="day"), ca_MIN=1:5, cb_MIN=3:7)
  Example_MAX &lt;- data.frame(Date=seq(as.Date("1979/1/1"),as.Date("1979/1/5"), by="day"), ca_MAX=2:6, cb_MAX=4:8)
  
  &gt;Example_MIN
  
      Date            ca_MIN cb_MIN
    1 1979-01-01      1      3
    2 1979-01-02      2      4
    3 1979-01-03      3      5
    4 1979-01-04      4      6
    5 1979-01-05      5      7
  </code></pre>
  
  <p>At the moment it works with:</p>
  
  <pre><code>Example_MIN &lt;-Example 
  Example_MAX &lt;-Example
  subset(ExampleMIN,select=-c(2,4))
  subset(ExampleMAX,select=-c(3,5))
  </code></pre>
  
  <p>This however gives me an error for the large dataset (although the outcome works for me):</p>
  
  <pre><code>Error in `[.data.table`(x, r, vars, with = FALSE) : j out of bounds
  </code></pre>
  
  <p>Is there a less cumbersome way to do this?</p>
  
  <p>I would prefer it also to work with dataset with a large number of columns &amp; with the column "_MIN" &amp; "_MAX" not sorted as odd and even columnnumber?</p>
  
  <p>I have seen post that are related, but I haven't managed this far to create a code that worked...</p>