Extract time only from POSIXlt object

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Extract time only from POSIXlt object

Simon Goodman
How can I extract only the time component from an POSIXlt object?

For example if I try the following it still returns both the date and time...

>as.POSIXlt(tr.date[1])
[1] "2010-10-18 21:46:53"
>as.POSIXlt(tr.date[1],"%H:%M:%S")
[1] "2010-10-18 21:46:53"

round and trunc don't help...  is there an "as.Time" equivalent to as.Date   ?

Thanks, Simon
Reply | Threaded
Open this post in threaded view
|

Re: Extract time only from POSIXlt object

David Winsemius

On Jan 29, 2011, at 7:45 PM, Simon Goodman wrote:

>
> How can I extract only the time component from an POSIXlt object?
>
> For example if I try the following it still returns both the date and
> time...
>
>> as.POSIXlt(tr.date[1])
> [1] "2010-10-18 21:46:53"
>> as.POSIXlt(tr.date[1],"%H:%M:%S")
> [1] "2010-10-18 21:46:53"
>
> round and trunc don't help...  is there an "as.Time" equivalent to  
> as.Date
> ?

 > ptime <- as.POSIXlt("2010-10-18 21:46:53")
 > ptime
[1] "2010-10-18 21:46:53"
 > format(ptime, format="%H:%M:%S")
[1] "21:46:53"

This is a character value, no longer a time.
--

David Winsemius, MD
West Hartford, CT

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extract time only from POSIXlt object

Jeff Newmiller
In reply to this post by Simon Goodman
Try

tt <- as.POSIXct("2011-01-29 15:00")
tt-trunc(tt,"days")

Note that this result can be used to add or subtract from datetime values, but I don't know of a convenient way to represent it as HH:MM:SS format.

"Simon Goodman" <[hidden email]> wrote:

>
>How can I extract only the time component from an POSIXlt object?
>
>For example if I try the following it still returns both the date and
>time...
>
>>as.POSIXlt(tr.date[1])
>[1] "2010-10-18 21:46:53"
>>as.POSIXlt(tr.date[1],"%H:%M:%S")
>[1] "2010-10-18 21:46:53"
>
>round and trunc don't help...  is there an "as.Time" equivalent to
>as.Date  
>?
>
>Thanks, Simon
>
>--
>View this message in context:
>http://r.789695.n4.nabble.com/Extract-time-only-from-POSIXlt-object-tp3246751p3246751.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>[hidden email] 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.

---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<[hidden email]>        Basics: ##.#.       ##.#.  Live Go...
                                      Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extract time only from POSIXlt object

Gabor Grothendieck
In reply to this post by Simon Goodman
On Sat, Jan 29, 2011 at 7:45 PM, Simon Goodman <[hidden email]> wrote:

>
> How can I extract only the time component from an POSIXlt object?
>
> For example if I try the following it still returns both the date and
> time...
>
>>as.POSIXlt(tr.date[1])
> [1] "2010-10-18 21:46:53"
>>as.POSIXlt(tr.date[1],"%H:%M:%S")
> [1] "2010-10-18 21:46:53"
>
> round and trunc don't help...  is there an "as.Time" equivalent to as.Date
> ?
>

If you are looking for a class that represents time of day, the
"times" class in the chron package can do that:

> lt <- as.POSIXlt("2010-10-18 21:46:53")
> library(chron)
> tt1 <- times(format(lt, "%H:%M:%S")); tt1
[1] 21:46:53

or

> tt2 <- unname(as.chron(lt) - dates(as.chron(lt))); tt2
[1] 21:46:53


Now tt1 and tt2 are "times" objects and can be manipulated:

> tt1 + 1/24 # add an hour
[1] 22:46:53


Although not specifically about times you might wish to read R News
4/1 which gives a bit of detail on chron and further references.


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Extract time only from POSIXlt object

Simon Goodman
In reply to this post by Simon Goodman
Those are all really helpful responses. Got it sorted now. Many thanks indeed!