title: Custom round number function
body: <p>I am trying to create a function (<code>my.func</code>) that could round a number/numeric vector/matrix/data.frame conditionaly to its value.</p>
  
  <p>For example: <code>my.func(1630.123)</code> will give 1630 and <code>my.func(0.123)</code> will return 0.12</p>
  
  <p>I have tried:</p>
  
  <pre><code>my.func &lt;- function(x0, ...) {
    unlist(lapply(x0, function(x) {
      if(x &gt;= 100) return(round(x, 0))
      if(x &gt;= 10 &amp; x &lt; 100) return(round(x, 1))
      if(x &gt;= 0.1 &amp; x &lt; 10) return(round(x, 2))
      if(x &gt;= 0.01 &amp; x &lt; 0.1) return(round(x, 3))
      if(x &gt;= 0.001 &amp; x &lt; 0.01) return(round(x, 4))
      if(x &gt;= 0.0001 &amp; x &lt; 0.001) return(round(x, 5))
      if(x &gt;= 0.00001 &amp; x &lt; 0.0001) return(round(x, 6))
      if(x &gt;= 0.000001 &amp; x &lt; 0.00001) return(round(x, 7))
      if(x &gt;= 0.0000001 &amp; x &lt; 0.000001) return(round(x, 8))
      if(x &lt; 0.0000001) return(x)
    }))
  }
  </code></pre>
  
  <p>Works good for number and a vector but not for matrix/data.frame.</p>
  
  <p>I could put <code>if</code>'s and loop for matrix/df but probably there is another way to do this, much faster and easier</p>
  
  <p>Another example:</p>
  
  <p><code>my.func(c(1.314315, 125135.1234, 0.0124133, .00000234))</code> expect to return 1.31, 125135, 0.012, 0.0000023</p>