title: Difference between apply and sapply for data frame columns?
body: <p>Could someone please explain the differences between how <code>apply()</code> and <code>sapply()</code> operate on the <em>columns</em> of a data frame?</p>
  
  <p>For example, when attempting to find the class of each column in a data frame, my first inclination is to use <code>apply</code> on the columns:</p>
  
  <pre><code>&gt; apply(iris, 2, class)
  Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
   "character"  "character"  "character"  "character"  "character" 
  </code></pre>
  
  <p>This is not correct, however, as some of the columns are numeric:</p>
  
  <pre><code>&gt; class(iris$Petal.Length)
  [1] "numeric"
  </code></pre>
  
  <p>A quick search on Google turned up <a href="https://stackoverflow.com/a/21125298/554531">this solution</a> for the problem which uses <code>sapply</code> instead of <code>apply</code>:</p>
  
  <pre><code>&gt; sapply(iris, class)
  Sepal.Length  Sepal.Width Petal.Length  Petal.Width      Species 
     "numeric"    "numeric"    "numeric"    "numeric"     "factor"
  </code></pre>
  
  <p>In this case, <code>sapply</code> is implicitly converting <code>iris</code> to a list, and then applying the function to each entry in the list, e.g.:</p>
  
  <pre><code>&gt; class(as.list(iris)$Petal.Length)
  [1] "numeric"
  </code></pre>
  
  <p>What I'm still unclear about is why my original attempt using <code>apply</code> didn't work. </p>