[R] format numbers without leading or trailing 0s

2011-11-29 Thread Michael Friendly
A simple question, but I can't find something to do what I want: Given: a vector of numbers, like lambda - c(0, 0.005, 0.01, 0.02, 0.04, 0.08) Desired: format them in minimal space for use as plot labels, ie, without leading or tailing 0s. For this example: lambdaf - c(0, .005, .01, .02,

Re: [R] format numbers without leading or trailing 0s

2011-11-29 Thread Sarah Goslee
Here's one way to get rid of leading zeros before the decimal point: gsub(^0\\., \\., as.character(lambda)) [1] 0.005 .01 .02 .04 .08 Sarah On Tue, Nov 29, 2011 at 12:04 PM, Michael Friendly frien...@yorku.ca wrote: A simple question, but I can't find something to do what I want:

Re: [R] format numbers without leading or trailing 0s

2011-11-29 Thread Bert Gunter
.. and if you want to simultaneously handle possible multiple trailing zeros (not sure whether this could even happen) (somewhat but not completely tested) lambda - c(0, 0.005, 0.01, 0.02, 0.04, 0.08) gsub(^0(\\..*[^0])0*$,\\1,lambda) [1] 0.005 .01 .02 .04 .08 Note that the

Re: [R] format numbers without leading or trailing 0s

2011-11-29 Thread William Dunlap
-project.org] On Behalf Of Bert Gunter Sent: Tuesday, November 29, 2011 9:57 AM To: Sarah Goslee Cc: R-help; Michael Friendly Subject: Re: [R] format numbers without leading or trailing 0s .. and if you want to simultaneously handle possible multiple trailing zeros (not sure whether this could

Re: [R] format numbers without leading or trailing 0s

2011-11-29 Thread Jeff Newmiller
Omitting the leading zero is dangerous, since the decimal point can disappear in a poor hardcopy leading to later misinterpretation. --- Jeff NewmillerThe . . Go Live...