[R] Formatting of time strings

David Winsemius dwinsemius at comcast.net
Sat Sep 11 00:56:08 CEST 2010


On Sep 10, 2010, at 6:41 PM, Dennis Fisher wrote:

> Colleagues,
>
> (OS X, R: 11.1)
>
> I am stuck on what should be a simple problem.
>
> I have time elements in numeric form (e.g., 14050.5).  I want to  
> convert these to HH:MM (e.g., 12:00).  I can do this with brute  
> force (7 commands) but I suspect that it can be accomplished with a  
> single command.  Any help would be appreciated.
>

?DateTimeClasses   #  POSIXct objects are internally the number of  
seconds past the origin

 > as.POSIXct(14050.5, origin="1970-01-01")
[1] "1970-01-01 03:54:10 EST"

# see ?strptime for format specs

 > format(as.POSIXct(14050.5, origin="1970-01-01"), format="%H:%M")  #  
which returns character vector
[1] "03:54"

 > str(format(as.POSIXct(14050.5 +(1:10)*3600, origin="1970-01-01"),  
format="%H:%M" ) )
  chr [1:10] "04:54" "05:54" "06:54" "07:54" ...

> Thanks.
>
> Dennis
>
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-866-PLessThan (1-866-753-7784)
> www.PLessThan.com
>
> ______________________________________________
> 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
West Hartford, CT



More information about the R-help mailing list