From 2c484745844905abd79ddf0a61a52291b07ca28e Mon Sep 17 00:00:00 2001 From: Taha Ahmed Date: Thu, 17 Mar 2011 00:02:26 +0100 Subject: [PATCH] Added the new function int2padstr(). Using a combination of format() and gsub(). The purpose is to simplify looping through many datafiles in a single report using for-loops. A way was needed to "convert" the for-loop's counting variable into a string of set length padded with zeroes. This function fills that need. --- common.R | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/common.R b/common.R index 3d20cf8..c5288b9 100644 --- a/common.R +++ b/common.R @@ -3,6 +3,7 @@ # Taha Ahmed, Jan 2011 # CONTENTS +# >>>> int2padstr # >>>> It2charge # >>>> ProvideSampleId # >>>> ConvertRefPot @@ -14,6 +15,29 @@ +################################################## +################## int2padstr #################### +################################################## +int2padstr <- function (ii, pchr, w) { + ## Description: + ## Converts an integer or a vector of integers to + ## a string padded with characters. + ## Usage: + ## int2padstr(ii, pchr, w) + ## Arguments: + ## ii: integer or vector of integers + ## pchr: a padding character (e.g., "0") + ## w: width of the return string (an integer) + ## Make sure to set the width longer than + ## or equal to the length of the biggest integer. + ## For example, if the integers (ii) are + ## in the range 1 - 100, set w to at least 3. + ## Value: + ## A character string or a vector of character strings + gsub(" ", pchr, formatC(ii, format="s", mode="character", width = w)) +} + + ################################################## ################### It2charge #################### ##################################################