title: How to avoid implicit character conversion when using apply on dataframe
body: <p>When using <code>apply</code> on a data.frame, the arguments are (implicitly) converted to character. An example:</p>
  
  <pre><code>df &lt;- data.frame(v=1:10, t=1:10)
  df &lt;- transform(df, t2 = as.POSIXlt(t, origin = "2013-08-13"))
  class(df$t2[1])
  ## [1] "POSIXct" "POSIXt" (correct)
  </code></pre>
  
  <p>but:</p>
  
  <pre><code> apply(df, 1, function(y) class(y["t2"]))
   ## [1] "character" "character" "character" "character" "character" "character"
   ## [7] "character" "character" "character" "character"
  </code></pre>
  
  <p>Is there any way to avoid this conversion? Or do I always have to convert back through <code>as.POSIXlt(y["t2"])</code>?</p>
  
  <p><strong>edit</strong><br>
  My df has 2 timestamps (say, t2 and t3) and some other fields (say, v1, v2). For each row with given t2, I want to find k (e.g. 3) rows with t3 closest to, but lower than t2 (and the same v1), and return a statistics over v2 from these rows (e.g. an average). I wrote a function f(t2, v1, df) and just wanted to apply it on all rows using <code>apply(df, 1, function(x) f(y["t2"], y["v1"], df)</code>. Is there any better way to do such things in R?</p>