[R] a decimal aligned column

Marc Schwartz (via MN) mschwartz at mn.rr.com
Thu Sep 28 23:49:52 CEST 2006


On Thu, 2006-09-28 at 12:31 -0700, BBands wrote:
> Hello,
> 
> For numbers in the range 100 to 100,000,000 I'd like to decimal align
> a right-justified comma-delineated column of numbers, but I haven't
> been able to work out the proper format statement. format(num,
> justify=right, width=15, big.mark=",") gets me close, but numbers
> larger than 1,000,000 project a digit beyond the right edge of the
> column, which I really don't understand. I gather I can get the
> decimal alignment from sprintf(), but I am not sure about the
> interaction of the two functions.
> 
> TIA,
> 
>     jab

Is this what you want?:

Nums <- 10 ^ (2:8)

Nums.Pretty <- format(Nums, width = 20, justify = "right", 
                      big.mark = ",", nsmall = 4, scientific = FALSE)


> Nums.Pretty
[1] "              100.0000" "            1,000.0000"
[3] "           10,000.0000" "          100,000.0000"
[5] "        1,000,000.0000" "       10,000,000.0000"
[7] "      100,000,000.0000"


> cat(Nums.Pretty, sep = "\n")
              100.0000
            1,000.0000
           10,000.0000
          100,000.0000
        1,000,000.0000
       10,000,000.0000
      100,000,000.0000



HTH,

Marc Schwartz



More information about the R-help mailing list