[R] Format with n significant figures / digits

wannymahoots wannymahoots at gmail.com
Tue Aug 19 14:41:33 CEST 2008


In case anyone has the same problem and wants a solution, here is mine
(albeit with some horrible hacks):

"my.signif" <- function(vec, digits=6) {
    # get correct number of significant figures
    vec = signif(vec, digits)

    # now get correct number of trailing zeros by calling sprintf and
specifying
    # the correct number of decimal places seperately for each
number
    om = floor(log10(abs(vec)))   # order of magnitude
    dp = digits-om-1              # no. decimal points
    dp[which(dp<0)] = 0           # use zero when dp is negative
    for(i in 1:length(vec)) {
        if(is.na(vec[i])) { next }
        vec[i] = sprintf(paste("%.",dp[i],"f", sep=""), vec[i])
    }
    return(vec)
}


On Aug 14, 3:23 pm, wannymaho... at gmail.com wrote:
> I am not sure that format solves the problem (although again I may  
> well be missing something)
>
> # trailing zeros on large numbers
>  >format(vec, digits=4, scientific=F)
> [1] "      0.8000" "    123.4567" "      0.1235" "      7.6543"  
> "7654321.0000"
>
> # misses trailing zeros
>  > format(vec[1], digits=4, scientific=F)
> [1] "0.8"
>
> I guess I could resort to writing a routine that called format with  
> different arguments depending on the magnitude of each number in the  
> vector, but I was hoping for a cleaner solution.  Any thoughts?
>
> On 14 Aug 2008, at 14:57, Prof Brian Ripley wrote:
>
>
>
> > I think you missed the function called 'format'.  R's internal print  
> > routines (used by format) calculate the format passed to (C level)  
> > sprintf based on the input, including 'digits'.  Just make sure you  
> > pass one number at a time to format() if you don't want a common  
> > layout for all the numbers.
>
> > On Thu, 14 Aug 2008, wannymaho... at gmail.com wrote:
>
> >> Hi everyone,
>
> >> I can't figure out how to format numbers to have a certain number  
> >> of significant figures (as opposed to decimal places) without using  
> >> scientific notation and including trailing zeros if necessary.
>
> >> e.g. I would like to achieve the following:
> >> 0.800001  --->  0.8000
> >> 123.4567    --->    123.4
> >> 0.1234567  --->    0.1234
> >> 7.654321    --->    7.654
> >> 7654321     --->    7654000
>
> >> It seems like it should be simple, but I can't seem to do this  
> >> using sprintf. Specifically, I get the following outputs:
>
> >>> vec = c(0.800001, 123.4567, 0.1234567, 7.654321, 7654321)
>
> >>> #  this specifies precision after decimal point, rather than  
> >>> significant figures
> >>> sprintf("%.4f", vec)
> >> [1] "0.8000"       "123.4567"     "0.1235"       "7.6543"  
> >> "7654321.0000"
>
> >>> #  uses scientific notation, but significant figures are correct
> >>> sprintf("%.3e", vec)
> >> [1] "8.000e-01" "1.235e+02" "1.235e-01" "7.654e+00" "7.654e+06"
>
> >>> #  correct number of significant figures, and without scientific  
> >>> notation, but has trailing zeros on large numbers
> >>> sprintf("%.4f", sprintf("%.3e", vec))
> >> [1] "0.8000"       "123.5000"     "0.1235"       "7.6540"  
> >> "7654000.0000"
>
> >> Am I missing something obvious and can someone help me?
>
> >> Many thanks
>
> >> ______________________________________________
> >> R-h... at r-project.org mailing list
> >>https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
>
> > --
> > Brian D. Ripley,                  rip... at stats.ox.ac.uk
> > Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> > University of Oxford,             Tel:  +44 1865 272861 (self)
> > 1 South Parks Road,                     +44 1865 272866 (PA)
> > Oxford OX1 3TG, UK                Fax:  +44 1865 272595
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list