[R] Using format to add leading zeroes

David Winsemius dwinsemius at comcast.net
Fri Mar 13 23:53:49 CET 2009


If you have a function that just works on one item at a time then  
sapply will cure that hormonal deficiency. It will even let you  
forward arguments to ... if such are allowed:

 > sapply(vec, "+", 1)
[1] 2 3 4 5 6
 > vec
[1] 1 2 3 4 5
 > sapply(vec, "+", 1)
[1] 2 3 4 5 6

-- 
David Winsemius, MD

On Mar 13, 2009, at 6:17 PM, Jonathan Greenberg wrote:

> 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
>
> -- 
>
> Jonathan A. Greenberg, PhD
> Postdoctoral Scholar
> Center for Spatial Technologies and Remote Sensing (CSTARS)
> University of California, Davis
> One Shields Avenue
> The Barn, Room 250N
> Davis, CA 95616
> Cell: 415-794-5043
> AIM: jgrn307, MSN: jgrn307 at hotmail.com, Gchat: jgrn307
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list