s-news
[Top] [All Lists]

Re: Formatting with Leading Zeroes

To: "'Mike Prager'" <Mike.Prager@noaa.gov>, S News Mailing List <s-news@wubios.wustl.edu>
Subject: Re: Formatting with Leading Zeroes
From: "CHASALOW, SCOTT [AG/2165]" <scott.chasalow@cereon.com>
Date: Thu, 21 Jun 2001 09:24:29 -0500
Here's my suggestion.

Cheers,
Scott

================================
Scott D. Chasalow
Cereon Genomics

Email: Scott.Chasalow@cereon.com
================================

"leadzero" <-
function(x, minimum.digits = 2)
{
# DATE WRITTEN:  30 Sep 1997             LAST REVISED:  30 Sep 1997
# AUTHOR:  Scott D. Chasalow  (Scott.Chasalow@cereon.com)
#
# DESCRIPTION:
#       Pad non-negative integers with leading zeroes. 
#
# REQUIRED ARGUMENTS:
# x     an integer vector. Results may be weird if x contains anything but
#       non-negative integers!
#
# OPTIONS ARGUMENTS:
# minimum.digits  a positive integer, giving the minimum number of digits to
#       require in each element of the output.
#
# VALUE:
#       a character vector, with every element having the same number
#       of characters, at least minimum.digits.
# 
     if(length(x) == 0) return(as.character(x))
     nc <- nchar(paste(x))
     howmany <- max(minimum.digits[1] - 1, max(nc) - 1)
     zeros <- paste(rep("0", howmany), collapse = "")
     substring(paste(zeros, x, sep = ""), nc)
}


-----Original Message-----
From: Mike Prager [mailto:Mike.Prager@noaa.gov]
Sent: Wednesday, 20 June, 2001 18:04
To: S News Mailing List
Subject: [S] Formatting with Leading Zeroes


S+ 2000 on Windows 2000.

 From an integer, I wish to derive a character string of a specific width, 
using leading zeroes as needed. How?

For example,  I would like to convert 4 to "0004" and 120 to "0120".

I know this is possible, and I think I've even done it before, but I can't 
for the life of me find out how. This is for opening a series of files with 
sequential names.

Thanks for all suggestions!

...Mike Prager

-- 
Michael Prager, Ph.D.                <Mike.Prager@noaa.gov>
NOAA Center for Coastal Fisheries and Habitat Research
Beaufort, North Carolina  28516
http://shrimp.ccfhrb.noaa.gov/~mprager/

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