[R] Time rather than dates?

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue May 30 14:26:27 CEST 2006


On Tue, 30 May 2006, Robert Lundqvist wrote:

> Using strptime() and other functions for dates has been very helpful with
> the kind of data I often work with. However, I haven't found out how time
> as such should be specified. All my attempts result in time *and* date:
>
>> treatment_time<-c("01:02:03","02:03:04") # hours:minutes:seconds
>> time.2<-strptime(treatment_time,format="%H:%M:%S")
>
>> time.2
> [1] "1900-01-01 01:02:03" "1900-01-01 02:03:04"
>
> Why the 1900-...? I had hoped for some easy conversion from time to
> numeric data and possibly back. Assistance would be appreciated.

You asked to print a datetime object of class POSIXlt, and unspecified 
fields are set to their earliest values.  But printing is only part of the 
story.

What do you actually want from this?

 	3600*time.2$hour + 60*time.2$min + time.2$sec

gives you the number of seconds since midnight, for example.  See
?DateTimeClasses

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list