Dear R experts, format(12345678, digits = 2) gives [1] "1.2e+07" while format(1234567, digits = 2) gives [1] "1234567" but I'd like the last number to be represented as "1.2e+06" string too. Where am I wrong? Thanks, Timur.
Dear Timur,
sprintf may be what you want:
sprintf("%.1e", 12345678)
sprintf("%.1e", 1234567)
Regards,
Andrew C. Ward
CAPE Centre
Department of Chemical Engineering
The University of Queensland
Brisbane Qld 4072 Australia
andreww at cheque.uq.edu.au
Quoting Timur Elzhov <Timur.Elzhov at jinr.ru>:
> Dear R experts,
>
> format(12345678, digits = 2)
>
> gives
> [1] "1.2e+07"
>
> while
> format(1234567, digits = 2)
>
> gives
> [1] "1234567"
>
> but I'd like the last number to be represented as
> "1.2e+06" string too.
>
> Where am I wrong?
>
> Thanks,
> Timur.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
Timur Elzhov <Timur.Elzhov at jinr.ru> wrote:> format(1234567, digits = 2) > [1] "1234567" > but I'd like the last number to be represented as "1.2e+06" string too.R chooses "1234567" instead of "1.2e+06" because it has fewer or equal number of characters (equal in this case). In R-devel (which will become R-1.8.0, as I understand it), an options(scipen) has been added which penalizes scientific notation by an additional number of characters. In your case, a negative penalty would do the trick: R-devel> options(scipen=-9) R-devel> format(1234567, digits = 2) [1] "1.2e+06" However, unless you feel like compiling a development version or waiting until 1.8.0, Andrew C. Ward's <s195404 at student.uq.edu.au> suggestion is probably the simpler way to go: Ward> sprintf("%.1e", 1234567) -- -- David Brahm (brahm at alum.mit.edu)