[R] Formatting numbers

Wacek Kusnierczyk Waclaw.Marcin.Kusnierczyk at idi.ntnu.no
Mon Apr 27 13:18:34 CEST 2009


baptiste auguie wrote:
> sprintf("%04.f", 2)
>

makes no sense to use the float specifier for ints here;  besides, it's
slower:

    x = 1:10^5

    library(rbenchmark)
    benchmark(columns=c('test', 'elapsed'), replications=100,
       sprintf('%05d', x),
       sprintf('%05.f', x))
    #                  test elapsed
    # 1  sprintf("%05d", x)   4.952
    # 2 sprintf("%05.f", x)  10.019


> sapply(sample(1:1000,4), function(ii) sprintf("%04.f",ii))

sprintf is vectorized!!!

>
> ?sprintf

indeed.

vQ




More information about the R-help mailing list