title: How to apply function returning data.frames with factors to sequence
body: <p>How do I apply a function which returns a data.frame with factors to a sequence?</p>
  
  <p>Example:</p>
  
  <pre><code>s &lt;- factor(c(10, 20, 30))
  t &lt;- factor(c("a", "b", "a"))
  v &lt;- c(5, 6, 4)
  
  df &lt;- data.frame(s,t,v)
  </code></pre>
  
  <p>So the data.frame df is this:</p>
  
  <pre><code>   s t v
  1 10 a 5
  2 20 b 6
  3 30 a 4
  </code></pre>
  
  <p>I also have a function which returns a data.frame:</p>
  
  <pre><code>simpleFunc2 &lt;- function(df, x){
    tmp &lt;- subset(df, df$s == x)
    return(tmp)
  }
  </code></pre>
  
  <p>Now I have a sequence</p>
  
  <pre><code>x &lt;- c(20, 30, 10, 30, 10)
  </code></pre>
  
  <p>and want to the result auf applying the function simpleFunc2 to this sequence.</p>
  
  <p>I use sapply </p>
  
  <pre><code>sapply(x, function(x) simpleFunc2(df, x))
  </code></pre>
  
  <p>But I get</p>
  
  <pre><code>  [,1]     [,2]     [,3]     [,4]     [,5]    
  s factor,1 factor,1 factor,1 factor,1 factor,1
  t factor,1 factor,1 factor,1 factor,1 factor,1
  v 6        4        5        4        5  
  </code></pre>
  
  <p>How do I get the right values of the factors back?</p>
  
  <p>This example is simplified. So maybe there's a much simpler way to do it in this case.</p>