Re: [R] convert one digit numbers to two digits one

2013-11-06 Thread David Winsemius

On Nov 6, 2013, at 8:25 AM, Alaios 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
 

?sprintf  # other options are linked from that page.

 
   [[alternative HTML version deleted]]
Sigh.
-- 

David Winsemius
Alameda, CA, USA

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert one digit numbers to two digits one

2013-11-06 Thread Bert Gunter
(Assuming I understand) tons of ways of doing this.

So I'll just point out the
?nchar
function, which you can use to count characters in your tail end and
paste a 0 if there's only one, e.g. via ifelse() .

-- Bert

On Wed, Nov 6, 2013 at 8:25 AM, Alaios ala...@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
 [[alternative HTML version deleted]]

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

(650) 467-7374

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert one digit numbers to two digits one

2013-11-06 Thread Marc Schwartz
On Nov 6, 2013, at 10:25 AM, Alaios ala...@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

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] convert one digit numbers to two digits one

2013-11-06 Thread Barry Rowlingson
All these suggestions of using 'sprintf' might be right but you might
be doing it wrong...

If you are working with times, then use the date/time classes and the
handy functions for working on them. Which means the lubridate
package, most likely.

Are these times part of a calendar time, or are they just clock times
without reference to any day, or are they durations in hours and
minutes?



On Wed, Nov 6, 2013 at 4:34 PM, Marc Schwartz marc_schwa...@me.com wrote:
 On Nov 6, 2013, at 10:25 AM, Alaios ala...@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

 __
 R-help@r-project.org mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
 and provide commented, minimal, self-contained, reproducible code.

__
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.