> my.lt <- strptime("2003-02-05 03:00:02", format="%Y-%m-%d %H:%M:%S")
> x <- data.frame(d=my.lt)
> class(x$d)
[1] "POSIXct" "POSIXt"
I don’t know why data.frame changed x$d from a POSIXlt object to a POSIXct one. Now if I do
> x$d = my.lt
Then I got what I want, but this is ugly. Can anybody tell me 1) Why this happened; and 2) How to initialize a data frame with one of its column being a POSIXlt in a neat way.
Thank you.
As it says in the 3rd paragraph of the Details section of
?data.frame:That means
as.data.frame.POSIXltis being called. It’s defined as:So that’s why it happened. I can’t think of a clean way to do it using the
data.frameconstructor, but here is a bit of a kludge:This converts your
POSIXltobject to a data.frame using thevectormethod. If you really want to confuse yourself later, you can also use thePOSIXctmethod: