Re: [R] Preserving the class of POSIXt objects

From: David Wolfskill <>
Date: Fri, 25 Mar 2011 14:37:04 -0700

On Fri, Mar 25, 2011 at 09:14:50PM +0000, Kenn Konstabel wrote:
> Dear all,
> I am working with a list of objects each of which contains two POSIXct
> objects (say, $Start and $End) and a number of different data in
> addition to that. Now an easy way to extract Start times of all object
> could be sapply(x, "[", "Start") but this converts them all to
> numeric, and so does sapply(x, "[[", "Start"). lapply preserves the
> class but is obviously a list rather than an atomic vector but using
> unlist is a way of getting rid of the class again. I know the vector
> can be converted back using as.POSIXct and that one can set the class
> attribute back to "POSIXct" but is there an easier trick that I can't
> just figure out? Thanks in advance for any suggestions.
> ...

As Gabor Grothendieck pointed out to me when I tried to read a value as numeric, then force it to POSIXct:

| This line in the code above produces an invalid object:
| class(df$time) <- "POSIXct"
| It should be:
| class(df$time) <- c("POSIXct", "POSIXt")


David H. Wolfskill
Depriving a girl or boy of an opportunity for education is evil.

See for my public key.

______________________________________________ mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code.

  • application/pgp-signature attachment: stored
Received on Fri 25 Mar 2011 - 21:42:14 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 Mar 2011 - 23:50:24 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive