title: Subset variables in data frame based on column type
body: <p>I need to subset data frame based on column type - for example from data frame with 100 columns I need to keep only those column with type <code>factor</code> or <code>integer</code>. I've written a short function to do this, but is there any simpler solution or some built-in function or package on CRAN?</p>
  
  <p>My current solution to get variable names with requested types:</p>
  
  <pre><code>varlist &lt;- function(df=NULL, vartypes=NULL) {
    type_function &lt;- c("is.factor","is.integer","is.numeric","is.character","is.double","is.logical")
    names(type_function) &lt;- c("factor","integer","numeric","character","double","logical")
    names(df)[as.logical(sapply(lapply(names(df), function(y) sapply(type_function[names(type_function) %in% vartypes], function(x) do.call(x,list(df[[y]])))),sum))]  
  }
  </code></pre>
  
  <p>The function <code>varlist</code> works as follows:</p>
  
  <ol>
  <li>For every requested type and for every column in data frame call "is.TYPE" function</li>
  <li>Sum tests for every variable (boolean is casted to integer automatically)</li>
  <li>Cast result to logical vector</li>
  <li>subset names in data frame</li>
  </ol>
  
  <p>And some data to test it:</p>
  
  <pre><code>df &lt;- read.table(file="http://archive.ics.uci.edu/ml/machine-learning-databases/statlog/german/german.data", sep=" ", header=FALSE, stringsAsFactors=TRUE)
  names(df) &lt;- c('ca_status','duration','credit_history','purpose','credit_amount','savings', 'present_employment_since','installment_rate_income','status_sex','other_debtors','present_residence_since','property','age','other_installment','housing','existing_credits', 'job','liable_maintenance_people','telephone','foreign_worker','gb')
  df$gb &lt;- ifelse(df$gb == 2, FALSE, TRUE)
  df$property &lt;- as.character(df$property)
  varlist(df, c("integer","logical"))
  </code></pre>
  
  <p>I'm asking because my code looks really cryptic and hard to understand (even for me and I've finished the function 10 minutes ago).</p>