[R] Help with printing fixed width

Jim Lemon jim at bitwrit.com.au
Sat Oct 10 04:40:58 CEST 2009


On 10/09/2009 11:36 PM, sahil seth wrote:
> Hello R users,
> I am writing a summary() for a custom class, and am to display the integers
> right justified,
> Say where x is the vector with integers, I am using the following:
> cat("\t",format(x),"\t"...other columns)
>
> this way I am trying to pass the format(x), to the cat function to display
> it,
> but still I am getting a kink(b/w the 9th and 10th row).
>     6      81      170      251      1.998026e-08
>     7      26      93      119      4.937587e-10
>     8      96      209      305      8.750939e-11
>     9      94      211      305      1.758285e-11
>       10      88      205      293      6.47853e-12
>       11      82      244      326      7.943691e-20
>       12      57      145      202      4.964509e-10
> Thanks,
>    
HI Sahil,
The "kink" is due to the fact that each row is being formatted 
separately. You may notice that there is a kink in row 7 as well, due to 
the same problem. What you may want to do is use formatC instead of 
format like this:

cat(formatC(x,width=10),...,rep(" ",6),<last number>,"\n",sep="")

Jim




More information about the R-help mailing list