[R] strip leading 0's
jim holtman
jholtman at gmail.com
Sun Jan 25 20:17:37 CET 2009
Does something like this help:
> x <- matrix(runif(25,-2,2), 5)
> x
[,1] [,2] [,3] [,4] [,5]
[1,] 0.6188957 -1.14716746 1.9046828 -1.9476897 1.96735448
[2,] -0.5872109 -1.48251061 0.9271700 0.8622643 -0.01762569
[3,] -0.9189594 -0.08752786 -0.5730924 -1.5872631 -0.06260190
[4,] 1.9707362 1.69629788 -0.2741052 -0.2148626 -1.30623066
[5,] 0.5339731 0.39504387 -1.4071538 0.5604042 1.01928378
> x.out <- capture.output(x)[-1]
> # remove row names
> x.new <- sub("^\\S+", "", x.out)
> # replace leading '0's
> x.new <- gsub(" -0", " - ", x.new)
> x.new <- gsub(" 0", " ", x.new)
> x.new
[1] " .6188957 -1.14716746 1.9046828 -1.9476897 1.96735448"
[2] " - .5872109 -1.48251061 .9271700 .8622643 - .01762569"
[3] " - .9189594 - .08752786 - .5730924 -1.5872631 - .06260190"
[4] " 1.9707362 1.69629788 - .2741052 - .2148626 -1.30623066"
[5] " .5339731 .39504387 -1.4071538 .5604042 1.01928378"
>
On Sun, Jan 25, 2009 at 9:58 AM, Ista Zahn <izahn at psych.rochester.edu> wrote:
> 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
>
> ______________________________________________
> 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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list