title: Avoid rbind()/cbind() conversion from numeric to factor
body: <p>I'm trying to build a dataset before plotting it. I decided to use function factory <code>gammaplot.ff()</code> and the first version of my code looks like this:</p>
  <pre><code>PowerUtility1d &lt;- function(x, delta = 4) {
    return(((x+1)^(1 - delta)) / (1 - delta))
  }
  PowerUtility1d &lt;- Vectorize(PowerUtility1d, &quot;x&quot;)
  
  # function factory allows multiparametrization of PowerUtility1d()
  gammaplot.ff &lt;- function(type, gamma) {
    ff &lt;- switch(type, 
                 original = function(x) PowerUtility1d(x/10, gamma),
                 pnorm_wrong = function(x) PowerUtility1d(2*pnorm(x)-1, gamma),
                 pnorm_right = function(x) PowerUtility1d(2*pnorm(x/3)-1, gamma)
                )
    ff
  }
  
  gammaplot.df &lt;- data.frame(type=numeric(), gamma=numeric(), 
                             x=numeric(), y=numeric())
  gammaplot.gamma &lt;- c(1.1, 1.3, 1.5, 2:7)
  gammaplot.pts &lt;- (-1e4:1e4)/1e3
  
  # building the data set
  for (gm in gammaplot.gamma) {
     for (tp in c(&quot;original&quot;, &quot;pnorm_wrong&quot;, &quot;pnorm_right&quot;)) {
       fpts &lt;- gammaplot.ff(tp, gm)(gammaplot.pts)    
       dataChunk &lt;- cbind(tp, gm, gammaplot.pts, fpts)
       colnames(dataChunk) &lt;- names(gammaplot.df)
       gammaplot.df &lt;- rbind(gammaplot.df, dataChunk)
     }
  }
  
  # rbind()/cbind() cast all data to character, but x and y are numeric
  gammaplot.df$x &lt;- as.numeric(as.character(gammaplot.df$x))
  gammaplot.df$y &lt;- as.numeric(as.character(gammaplot.df$y))
  </code></pre>
  <p>Turns out, the whole data frame contains character data, so I have to convert it back manually (took me a while to discover that in the first place!). SO search <a href="https://stackoverflow.com/questions/9518740/why-rbind-converts-a-list-of-numeric-elements-to-a-character-matrix">indicates</a> that this happens because <strong>type</strong> variable is character. To avoid this (you can imagine performance issues on character data while building the data set!) I changed the code a bit:</p>
  <pre><code>gammaplot.ff &lt;- function(type, gamma) {
    ff &lt;- switch(type, 
                 function(x) PowerUtility1d(x/10, gamma),
                 function(x) PowerUtility1d(2*pnorm(x)-1, gamma),
                 function(x) PowerUtility1d(2*pnorm(x/3)-1, gamma)
                )
    ff
  }
  
  for (gm in gammaplot.gamma) {
    for (tp in 1:3) {
      fpts &lt;- gammaplot.ff(tp, gm)(gammaplot.pts)    
      dataChunk &lt;- cbind(tp, gm, gammaplot.pts, fpts)
      colnames(dataChunk) &lt;- names(gammaplot.df)
      gammaplot.df &lt;- rbind(gammaplot.df, dataChunk)
    }
  }
  </code></pre>
  <p>This works fine for me, but I lost a self-explanatory character parameter, which is a downside. Is there a way to keep the first version of function factory without an implicit conversion of all data to character?</p>
  <p>If there's another way of achieving the same result, I'd be happy to try it out.</p>