[R] strip leading 0's

Ista Zahn izahn at psych.rochester.edu
Sun Jan 25 15:58:14 CET 2009


Thanks for the suggestion replacing the leading 0 with a space instead
of nothing to preserve the layout, and for explaining why there is no
option for this.

Yes, I see why this sounds like a bad idea. The reason I asked is that
I use Sweave to write statistical reports, and I like to get the
formatting as close as possible without editing the .tex file
afterward. In my field (psychology) it is standard practice to omit
the leading zeros when reporting statistics whose value cannot exceed
1, mainly correlations and p values.

Thanks,
Ista
On Sun, Jan 25, 2009 at 2:39 AM, Prof Brian Ripley
<ripley at stats.ox.ac.uk> wrote:
>
> The only comprehensive way to do this would be to change R's internaal print mechanisms.  (Note that changing 0. to . breaks the layout: changing '0.' to ' .' would be better.)
>
> But you haven't told use why you would want to do this.  Leaving off leading zeroes makes output harder to read for most people, and indded leading periods are easy to miss (much easier than failing to see that you were asked not to send HTML mail).
>
> It would be easy for the cognescenti to add an option to R, but I suspect they all would need a lot of convincing to do so.
>
> On Sat, 24 Jan 2009, Ista Zahn wrote:
>
>> Dear all,
>> Is there a simple way to strip the leading "0"'s from R output? For example,
>> I want
>> Data <- data.frame(x=rnorm(10), y=x*rnorm(10), z = x+y+rnorm(10))
>> cor(Data)
>>
>> to give me
>>          x          y          z
>> x  1.0000000 -.1038904 -.3737842
>> y -.1038904  1.0000000  .4414706
>> z -.3737842  .4414706  1.0000000
>>
>> Several of you were kind enough to alert me to the existence of gsub a few
>> weeks ago, so I can do
>> gsub("0\\.","\\.",cor(Data))
>>
>> but I'm hoping someone has a better way (e.g, one that returns an object of
>> the same class as the original, preserving dimnames, etc.)
>>
>> Thanks,
>> Ista
>>
>>        [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> --
> Brian D. Ripley,                  ripley 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




More information about the R-help mailing list