Skip to content
Back to formatted view

Raw Message

Message-ID: <A518F2CE-82AB-462E-8044-5201608386BC@me.com>
Date: 2013-11-06T16:34:03Z
From: Marc Schwartz
Subject: convert one digit numbers to two digits one
In-Reply-To: <1383755123.64417.YahooMailNeo@web125304.mail.ne1.yahoo.com>

On Nov 6, 2013, at 10:25 AM, Alaios <alaios at yahoo.com> wrote:

> Hi all,
> the following returns the hour and the minutes
> 
> paste(DataSet$TimeStamps[selectedInterval$start,4], DataSet$TimeStamps[selectedInterval$start,5],sep=":")
> [1] "12:3"
> 
> the problem is that from these two I want to create a time stamp so 12:03. The problem is that the number 3 is not converted to 03. Is there an easy way when I have one digit integer to add a zero in the front? Two digits integers are working fine so far, 12:19, or 12:45 would appear correctly
> 
> I would like to thank you in advance for your help
> 
> Regards
> Alex


This is an example where using ?sprintf gives you more control:

> sprintf("%02d:%02d", 12, 3)
[1] "12:03"

> sprintf("%02d:%02d", 9, 3)
[1] "09:03"


The syntax '%02d' tells sprintf to print the integer and pad with leading zeroes to two characters where needed.

Regards,

Marc Schwartz