Skip to content
Prev 173694 / 398503 Next

Using format to add leading zeroes

I have a numerical vector which contains a (poorly) formatted time 
column, which, in theory, should be HHMM, but was distributed as an 
integer, so, for 12:15 am, it is saved as "15" (e.g. HHMM = 0015 with 
the zeroes stripped).  I'm trying to use this in conjunction with 
strptime, but I'm thinking because each time is an integer ranging from 
1 to 4 digits, I probably need to coerce this vector using "format" 
first to add back the leading zeroes -- but I can't seem to figure out 
the specific parameters to send to format().  Thoughts?  I could use the 
pad0 function in PBSmodelling, but it seems to only work on one item at 
a time (not an entire vector) so I thought I'd ask here if there's an 
easy trick to getting where I need to go using on the base package and 
not needing to write a for-next loop to implement pad0.

Thanks!

--j