Re: [R] add leading zeros

2012-08-07 Thread Liviu Andronic
On Tue, Aug 7, 2012 at 10:48 PM, David Winsemius wrote: > On Aug 7, 2012, at 3:55 AM, Liviu Andronic wrote: >> For anyone interested, I came up with a small wrapper for the above: >> add.lead <- function(x, width=max(nchar(x))){ >>sprintf(paste('%0', width, 'i', sep=''), x) >> } > > Thanks, Li

Re: [R] add leading zeros

2012-08-07 Thread David Winsemius
On Aug 7, 2012, at 3:55 AM, Liviu Andronic wrote: Hello On Fri, Jul 27, 2012 at 6:54 AM, R. Michael Weylandt wrote: Much easier than you think: x <- c(1L, 9000L) sprintf("%05i",x) For anyone interested, I came up with a small wrapper for the above: add.lead <- function(x, width=max(ncha

Re: [R] add leading zeros

2012-08-07 Thread Liviu Andronic
Hello On Fri, Jul 27, 2012 at 6:54 AM, R. Michael Weylandt wrote: > Much easier than you think: > > x <- c(1L, 9000L) > > sprintf("%05i",x) > For anyone interested, I came up with a small wrapper for the above: add.lead <- function(x, width=max(nchar(x))){ sprintf(paste('%0', width, 'i', sep

Re: [R] add leading zeros

2012-07-27 Thread ROLL Josh F
This solution was the most elegant. Thanks everyone. Josh -Original Message- From: R. Michael Weylandt [mailto:michael.weyla...@gmail.com] Sent: Thursday, July 26, 2012 9:55 PM To: ROLL Josh F Cc: r-help@r-project.org Subject: Re: [R] add leading zeros Much easier than you think

Re: [R] add leading zeros

2012-07-27 Thread arun
ot; "-003" "-002" "-001" "" A.K. - Original Message ----- From: Rui Barradas To: LCOG1 Cc: r-help Sent: Friday, July 27, 2012 7:03 AM Subject: Re: [R] add leading zeros Hello, Here is a function that I wrote some time ago. # pad with

Re: [R] add leading zeros

2012-07-27 Thread arun
uot; "00-5" "00-6" "00-7" "00-8" [11] "00-9" "0-10"  formatC(1:-10,width=4,format="d",flag="0")  [1] "0001" "" "-001" "-002" "-003" "-004" "-005"

Re: [R] add leading zeros

2012-07-27 Thread Rui Barradas
Hello, Here is a function that I wrote some time ago. # pad with zeros padz <- function(x, width=max(nchar(x)), fill="0") gsub(" ", fill, formatC(x, width=width)) ##- test padz( c(1, 10, 100) ) padz(1:10, 4) ##- non-sense padz(-5:0, 4) padz(runif(10)) Hope this help

Re: [R] add leading zeros

2012-07-26 Thread Jorge I Velez
Hi Josh, Check ?formatC and ?sprintf for some options. Regards, Jorge.- Sent from my phone. Please excuse my brevity and misspelling. On Jul 27, 2012, at 12:49 AM, LCOG1 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 ad

Re: [R] add leading zeros

2012-07-26 Thread R. Michael Weylandt
Much easier than you think: x <- c(1L, 9000L) sprintf("%05i",x) Best, Michael On Thu, Jul 26, 2012 at 8:08 PM, LCOG1 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 > charac

[R] add leading zeros

2012-07-26 Thread LCOG1
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 "1" or "9000" I want "09000". I tried vec <- 1:1000 sapply(vec, FUN = sprintf(paste