r - Custom round number function -


i trying create function (my.func) round number/numeric vector/matrix/data.frame conditionaly value.

for example: my.func(1630.123) give 1630 , my.func(0.123) return 0.12

i have tried:

my.func <- function(x0, ...) {   unlist(lapply(x0, function(x) {     if(x >= 100) return(round(x, 0))     if(x >= 10 & x < 100) return(round(x, 1))     if(x >= 0.1 & x < 10) return(round(x, 2))     if(x >= 0.01 & x < 0.1) return(round(x, 3))     if(x >= 0.001 & x < 0.01) return(round(x, 4))     if(x >= 0.0001 & x < 0.001) return(round(x, 5))     if(x >= 0.00001 & x < 0.0001) return(round(x, 6))     if(x >= 0.000001 & x < 0.00001) return(round(x, 7))     if(x >= 0.0000001 & x < 0.000001) return(round(x, 8))     if(x < 0.0000001) return(x)   })) } 

works number , vector not matrix/data.frame.

i put if's , loop matrix/df there way this, faster , easier

another example:

my.func(c(1.314315, 125135.1234, 0.0124133, .00000234)) expect return 1.31, 125135, 0.012, 0.0000023

you want signif rounds off given number of significant digits.

x <- runif(16) * 10^(7*runif(16)-5) cbind(x, mx = my.func(x), sx = signif(x, 3)) 
                 x       mx       sx  [1,] 1.395044e-01 1.40e-01 1.40e-01  [2,] 9.751368e-06 9.80e-06 9.75e-06  [3,] 3.451619e-04 3.50e-04 3.45e-04  [4,] 2.203204e-03 2.20e-03 2.20e-03  [5,] 6.660684e-05 6.70e-05 6.66e-05  [6,] 3.143732e-02 3.10e-02 3.14e-02  [7,] 1.976514e-05 2.00e-05 1.98e-05  [8,] 3.747693e+00 3.75e+00 3.75e+00  [9,] 4.099091e-03 4.10e-03 4.10e-03 [10,] 3.124711e-02 3.10e-02 3.12e-02 [11,] 3.162478e-04 3.20e-04 3.16e-04 [12,] 1.029170e-05 1.00e-05 1.03e-05 [13,] 6.746715e-04 6.70e-04 6.75e-04 [14,] 7.667078e-03 7.70e-03 7.67e-03 [15,] 2.002506e-03 2.00e-03 2.00e-03 [16,] 2.164340e-03 2.20e-03 2.16e-03 

or there special reason wanting digit above 0? i.e. why don't want

if(x >= 10 & x < 100) return(round(x, 1)) if(x >= 1 & x < 10) return(round(x, 2)) if(x >= 0.1 & x < 1) return(round(x, ?))   # <----- line if(x >= 0.01 & x < 0.1) return(round(x, 3)) if(x >= 0.001 & x < 0.01) return(round(x, 4)) 

update

if want function output numbers nice read suggest round off digits signif, print sprintf, , trim off tailing zeros , decimal separators sub.

sub("\\.$", "", sub(".0+$", "", sprintf("%f", signif(x, 3)))) 
 [1] "0.000226" "3.8"      "363"      "1380"     "0.0016"   "0.000331"  [7] "0.000047" "0.20"     "0.047"    "105"      "22"       "0.000013" [13] "0.000013" "0.054"    "2.6"      "0.31" 

another function might of interest in formatc, you'd have bear scientific notation large or small values then.

formatc(x, digits=3) 
 [1] "0.000226" "3.82"     " 363"     "1.38e+03" "0.00163"  "0.000331"  [7] "4.68e-05" "0.208"    "0.0479"   " 105"     "22.6"     "1.25e-05" [13] "1.25e-05" "0.0542"   "2.69"     "0.311" 

Comments