title: Apply function to each row of data.frame and preserve column classes
body: <p>I wonder if there is a way to apply a function to each row of a data.frame such that the column classes are preserved? Let's look at an example to clarify what I mean:</p>
  
  <pre><code>test &lt;- data.frame(startdate = as.Date(c("2010-03-07", "2013-09-13", "2011-11-12")),
                     enddate = as.Date(c("2010-03-23", "2013-12-01", "2012-01-05")),
                     nEvents = c(123, 456, 789))
  </code></pre>
  
  <p>Suppose I would like to expand the data.frame <code>test</code> by inserting all days between <code>startdate</code> and <code>enddate</code> and distribute the number of events over those days. My first try to do so was this:</p>
  
  <pre><code>eventsPerDay1 &lt;- function(row) {
      n_days &lt;- as.numeric(row$enddate - row$startdate) + 1
      data.frame(date = seq(row$startdate, row$enddate, by = "1 day"),
                 nEvents = rmultinom(1, row$nEvents, rep(1/n_days, n_days)))
  }
  
  apply(test, 1, eventsPerDay1)
  </code></pre>
  
  <p>This, however, is not possible because <code>apply</code> calls <code>as.matrix</code> on <code>test</code> and thus it gets converted to a character matrix and all column classes are lost.</p>
  
  <p>I already found two workarounds which you can find below, so my question is more of a philosphical nature.</p>
  
  <pre><code>library(magrittr)
  ############# Workaround 1
  eventsPerDay2 &lt;- function(startdate, enddate, nEvents) {
      n_days &lt;- as.numeric(enddate - startdate) + 1
      data.frame(date = seq(startdate, enddate, by = "1 day"),
                 nEvents = rmultinom(1, nEvents, rep(1/n_days, n_days)))
  }
  
  mapply(eventsPerDay2, test$startdate, test$enddate, test$nEvents, SIMPLIFY = F) %&gt;%
      do.call(rbind, .)
  
  
  ############# Workaround 2
  seq_along(test) %&gt;%
      lapply(function(i) test[i, ]) %&gt;%
      lapply(eventsPerDay1) %&gt;%
      do.call(rbind, .)
  </code></pre>
  
  <p>My "problem" with the workarounds is the following:</p>
  
  <ul>
  <li>Workaround 1: It may not be the best reason, but I simply do not like <code>mapply</code>. It has a different signature than the other <code>*apply</code> functions (as the the order of arguments differs) and I always feel that a <code>for</code> loop would just have been clearer.</li>
  <li>Workaround 2: While being very flexible, I think it is not clear at first sight what is happening.</li>
  </ul>
  
  <p>So does anyone know a function whose call would look like <code>apply(test, 1, eventsPerDay1)</code> and that will work?</p>