[R] Time rather than dates?

2006-05-30 Thread Robert Lundqvist
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.

Robert

__
R-help@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


Re: [R] Time rather than dates?

2006-05-30 Thread Prof Brian Ripley
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,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@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


Re: [R] Time rather than dates?

2006-05-30 Thread Gabor Grothendieck
Try using the "times" class in the chron package.

> library(chron)
> times(c("01:02:03","02:03:04"))
[1] 01:02:03 02:03:04

On 5/30/06, Robert Lundqvist <[EMAIL PROTECTED]> 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.
>
> Robert
>
> __
> R-help@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
>

__
R-help@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


Re: [R] Time rather than dates?

2006-05-30 Thread Petr Pikal
Hi

Which version of R do you use?
Version 2.3.1 beta (2006-05-23 r38179)

> treatment_time<-c("01:02:03","02:03:04")
> strptime(treatment_time,format="%H:%M:%S")
[1] "2006-05-30 01:02:03" "2006-05-30 02:03:04"
> ttt<-strptime(treatment_time,format="%H:%M:%S")

For changing format of POSIX variables use format
> format(ttt, "%H:%M")
[1] "01:02" "02:03"

Maybe also consult locale setting.

HTH
Petr



On 30 May 2006 at 14:16, Robert Lundqvist wrote:

To: r-help@stat.math.ethz.ch
From:   Robert Lundqvist <[EMAIL PROTECTED]>
Date sent:  Tue, 30 May 2006 14:16:29 +0200
Subject:[R] Time rather than dates?
Send reply to:  [EMAIL PROTECTED]
<mailto:[EMAIL PROTECTED]>
<mailto:[EMAIL PROTECTED]>

> 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.
> 
> Robert
> 
> __
> R-help@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

Petr Pikal
[EMAIL PROTECTED]

__
R-help@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


Re: [R] Time rather than dates?

2006-05-30 Thread Prof Brian Ripley
On Tue, 30 May 2006, Petr Pikal wrote:

> Which version of R do you use?
> Version 2.3.1 beta (2006-05-23 r38179)
>
>> treatment_time<-c("01:02:03","02:03:04")
>> strptime(treatment_time,format="%H:%M:%S")
> [1] "2006-05-30 01:02:03" "2006-05-30 02:03:04"

>From ?strptime

  If the date string does not specify the date completely, the
  returned answer may be system-specific.  The most common behaviour
  is to assume that unspecified seconds, minutes or hours are zero,
  and a missing year, month or day is the current one.

which explains the different answer.  Neither of you told us your OS.

>> ttt<-strptime(treatment_time,format="%H:%M:%S")
>
> For changing format of POSIX variables use format
>> format(ttt, "%H:%M")
> [1] "01:02" "02:03"
>
> Maybe also consult locale setting.
>
> HTH
> Petr
>
>
>
> On 30 May 2006 at 14:16, Robert Lundqvist wrote:
>
> To:       r-help@stat.math.ethz.ch
> From: Robert Lundqvist <[EMAIL PROTECTED]>
> Date sent:Tue, 30 May 2006 14:16:29 +0200
> Subject:  [R] Time rather than dates?
> Send reply to:[EMAIL PROTECTED]
>   <mailto:[EMAIL PROTECTED]>
>   <mailto:[EMAIL PROTECTED]>
>
>> 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.
>>
>> Robert

-- 
Brian D. Ripley,  [EMAIL PROTECTED]
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, UKFax:  +44 1865 272595

__
R-help@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