Message-ID: <CAAmySGPN0B6iNNfHY31gL7O3RnF0-x33-MA6SwSDLQYugFEs7Q@mail.gmail.com>
Date: 2012-07-27T04:54:34Z
From: R. Michael Weylandt
Subject: add leading zeros
In-Reply-To: <1343351332302-4638031.post@n4.nabble.com>
Much easier than you think:
x <- c(1L, 9000L)
sprintf("%05i",x)
Best,
Michael
On Thu, Jul 26, 2012 at 8:08 PM, LCOG1 <jroll at lcog.org> wrote:
> Hi all,
> Trying to use an apply to add leading zeros to a set of values in a
> given vector. I only want to add enough zeros so that the total number of
> characters is 5, so if I have an element "1" i want "00001" or "9000" I want
> "09000". I tried
>
> vec <- 1:1000
> sapply(vec, FUN = sprintf(paste("%0",(5-nchar(x)),"d",sep=""),x))
>
> but she doesnt work. I struggle with the apply family for sure. Thanks
>
> Josh
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/add-leading-zeros-tp4638031.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.