-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-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 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 as.character() coercion is done automatically (and is
documented to be).
If you do much of this, it's worth going through one of the many web
tutorials on regular expressions. And if you're a minimalist like me,
you may even find R's man page, ?regexp), suffices.
Cheers,
Bert
On Tue, Nov 29, 2011 at 9:09 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:
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 <friendly at yorku.ca> wrote:
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", ".04", ".08")
--