[R] add leading zeros
ROLL Josh F
JRoll at lcog.org
Fri Jul 27 18:23:48 CEST 2012
This solution was the most elegant. Thanks everyone.
From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com]
Sent: Thursday, July 26, 2012 9:55 PM
To: ROLL Josh F
Cc: r-help at r-project.org
Subject: Re: [R] add leading zeros
Much easier than you think:
x <- c(1L, 9000L)
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.
> View this message in context:
> Sent from the R help mailing list archive at Nabble.com.
> R-help at r-project.org mailing list
> PLEASE do read the posting guide
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help