[R] Convert Time from HH.mm to Decimal hours?

jim holtman jholtman at gmail.com
Sun May 6 00:19:49 CEST 2007


Is this what you want?

> x <- seq(as.POSIXct("2006-05-05 12:00"), by="17 min", length=20)
> x
 [1] "2006-05-05 12:00:00 GMT" "2006-05-05 12:17:00 GMT" "2006-05-05
12:34:00 GMT"
 [4] "2006-05-05 12:51:00 GMT" "2006-05-05 13:08:00 GMT" "2006-05-05
13:25:00 GMT"
 [7] "2006-05-05 13:42:00 GMT" "2006-05-05 13:59:00 GMT" "2006-05-05
14:16:00 GMT"
[10] "2006-05-05 14:33:00 GMT" "2006-05-05 14:50:00 GMT" "2006-05-05
15:07:00 GMT"
[13] "2006-05-05 15:24:00 GMT" "2006-05-05 15:41:00 GMT" "2006-05-05
15:58:00 GMT"
[16] "2006-05-05 16:15:00 GMT" "2006-05-05 16:32:00 GMT" "2006-05-05
16:49:00 GMT"
[19] "2006-05-05 17:06:00 GMT" "2006-05-05 17:23:00 GMT"
> # convert to decimal hours -- assuming for the same day
> x.lt <- as.POSIXlt(x)
> x.hhdd <- x.lt$hour + x.lt$min/60 + x.lt$sec/3600
> x.hhdd
 [1] 12.00000 12.28333 12.56667 12.85000 13.13333 13.41667 13.70000
13.98333 14.26667 14.55000 14.83333
[12] 15.11667 15.40000 15.68333 15.96667 16.25000 16.53333 16.81667
17.10000 17.38333
>

On 5/5/07, Alan E. Davis <lngndvs at gmail.com> wrote:
> I have data in Unixtime that I wish to plot several ways.  I have
> learned to convert to a vector of time strings, and for the sake of
> plotting times of day vs. heights of tide (to examine seasonality
> component) I can extract HH:mm pretty swiftly and readily from
> Unixtime using emacs calc.  However, I have had trouble with decimal
> hours that are needed (or perhaps not) for at least one of the polar
> plotting packages.
>
> Is there an easy way to do this in R?
>
> Thank you for any ideas.  (I have learned how to do simple polar plots
> by way of a couple of people on this list.  Thanks very much.)
>
> Alan
>
> --
> Alan Davis, Kagman High School, Saipan  lngndvs at gmail.com
>
> I consider that the golden rule requires that if I like a program I
> must share it with other people who like it.
> --------Richard Stallman
>
> Every great advance in natural knowledge has involved the absolute
> rejection of authority.  ----- Thomas H. Huxley
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>


-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?



More information about the R-help mailing list