[R] Times and Dates
Duncan Murdoch
murdoch.duncan at gmail.com
Tue Apr 1 19:58:23 CEST 2014
On 01/04/2014 12:33 PM, Doran, Harold wrote:
> Is the time and date package the right one to convert a vector, such as the following, into a time format?
>
> "12:06" "11:51" "11:53" "12:27" "14:20" "12:27"
>
> The aim is to deal with a time variable numerically (find means, etc).
I don't know which package you were referring to. You can convert
strings to POSIXlt objects using the base package strptime function.
Those aren't numbers, but if you further convert them to POSIXct
objects, they are. The mean() function works on either type.
For example,
> x <- strptime( c("12:06", "11:51") , format="%H:%M")
> x
[1] "2014-04-01 12:06:00 EDT" "2014-04-01 11:51:00 EDT"
> mean(x)
[1] "2014-04-01 11:58:30 EDT"
You may get different results for the day and time zone. If you don't
want to see those, format the output:
> format(mean(x), format="%H:%M")
[1] "11:58"
Duncan Murdoch
More information about the R-help
mailing list