s-news
[Top] [All Lists]

Re: Padding out a vector of characters to a constant width

To: <Glenn.Treacy@ILIM.COM>
Subject: Re: Padding out a vector of characters to a constant width
From: "Thomas Jagger" <tjagger@blarg.net>
Date: Mon, 16 May 2005 19:50:22 -0600
Cc: <s-news@lists.biostat.wustl.edu>
In-reply-to: <946C68E53B08A84283BD1639AF350BBADF8111@ilife118.ilim.com>
Thread-index: AcVaIM3pUHglPDurSXimFwInnr9O0gAYAq6Q
This is a simple pad function that appends a character, can be used to
format numbers as well.

Do you mean something like: (V6.2 Windows Xp)

padd<-function(X,char="X",n=3) 
paste(substring(paste(rep(char,n),collapse=""),1,n-nchar(X)),X,sep="")

###Uses:
padd(c("A","AA","B","DDD"),"X",3)
[1] "XXA" "XAA" "XXB" "DDD"

padd(as.character(c(1,10,100,1000,99)),"0",4)
[1] "0001" "0010" "0100" "1000" "0099"

for generating reasonable character string labels that can be sorted and
maintain numerical ordering, e.g.

paste("Mylabel", padd(as.character(c(1,10,100,1000,99)),"0",4),sep=".")
[1] "Mylabel.0001" "Mylabel.0010" "Mylabel.0100" "Mylabel.1000"
"Mylabel.0099"

-----Original Message-----
From: s-news-owner@lists.biostat.wustl.edu
[mailto:s-news-owner@lists.biostat.wustl.edu] On Behalf Of
Glenn.Treacy@ILIM.COM
Sent: Monday, May 16, 2005 8:08 AM
To: s-news@lists.biostat.wustl.edu
Subject: Re: [S] Padding out a vector of characters to a constant width

Hi,

I have a vector characters and I want to padd these out with a fixed
character so that each element of the vector has the same length.

If my vector is "A","AA","B","DDD" and my padding character is "X" then I
want to return the vector "XXA","XAA","XXB","DDD", so each element has a
length of 3. I have solved this problem with the following two functions but
I can't help thinking that there must be a simpler method?

f.add.leading.chars<-function(jj.vec)
{
        # maximum number of characters
        no.char <- max(nchar(jj.vec))
        # make vector of necessary zeros
        vec <- rep(no.char, length(jj.vec)) - nchar(jj.vec)
        jj.pad <- unlist(lapply(1:length(jj.vec), f.pad, vec = vec, char =
"0"
                ))
        return(paste(jj.pad, jj.vec, sep = ""))
}

 f.pad<-function(x, vec, char)
{
        return(paste(rep(char, vec[x]), collapse = ""))
}

Regards,
Glenn

I am using S+6.2 on NT4


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.
This footnote also confirms that this email message has been swept 
for the presence of computer viruses.
**********************************************************************

--------------------------------------------------------------------
This message was distributed by s-news@lists.biostat.wustl.edu.  To
unsubscribe send e-mail to s-news-request@lists.biostat.wustl.edu with
the BODY of the message:  unsubscribe s-news


<Prev in Thread] Current Thread [Next in Thread>