[R] produce hours greater than 23
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Jan 5 02:24:10 CET 2006
In thinking about this some more here is an even shorter version of
hms. Note that %02.f produces at least 2 digits using leading
zeros to fill in if necessary and %5.5s truncates the string to the
first 5 characters (since our string is of the form mm:ss:hh and we only
need the mm:ss part).
hms2 <- function(x) sprintf("%02.f:%5.5s", floor(24*x), format(x%%1, "m:s:h"))
On 1/4/06, Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> The chron package has a "times" class that represents times in
> days and fractions of a day. See the help desk article in R News 4/1
> and the references therein for more info. Using "times" class
> the following one-line function, hms, will display produce the
> hours:mm:ss format:
>
> library(chron)
> hms <- function(x)
> sub("..:", ":", sprintf("%02.f%s", floor(24*x), format(x%%1)))
>
> # test this by creating a vector 3 "times" objects
>
> x <- structure(pi*seq(3), class = "times")
> hms(x) # "75:23:54" "150:47:47" "226:11:41"
>
>
> On 1/4/06, herodote at oreka.com <herodote at oreka.com> wrote:
> > Hy all,
> >
> > I wish to use the date function to draw againt the lifetime of a motor.
> >
> > This lifetime is given to me in Hours (it can go over 5000 hours)
> >
> > I'm unable to find how to convert this lifetime value to something like %H:%M:%S because when R see 24H it says 1 day, i don't want that, i just want %H:%M:%S with a value of %H higher than 24...
> > for example:
> > i've got this value in hours: 345.05 H
> >
> > I wish that R gives me : "345:3:0" or "345:03:00"
> >
> > What R function could do it?
> >
> > I've search for as.Date strptime... but none of these seems to be able to put a value for %H greater than 23.
> >
> > i've browse the help and docs and found nothing (am i blind?).
> >
> > thks all
> > guillaume.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
>
More information about the R-help
mailing list