[R] format numbers without leading or trailing 0s

Sarah Goslee sarah.goslee at gmail.com
Tue Nov 29 18:09:55 CET 2011


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")
>
> --


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list