[R] POSIXlt converted to POSIXct in as.data.frame()
Roger Bivand
Roger.Bivand at nhh.no
Thu Nov 9 20:54:45 CET 2006
In trying to use as.Date(), I've come across the conversion of POSIXlt to
POSIXct when a POSIXlt variable is included in a data frame:
my_POSIX <- strptime(c("11-09-2006", "11-10-2006", "11-11-2006",
"11-12-2006", "11-13-2006"), "%m-%d-%Y")
str(my_POSIX)
my_Date <- as.Date(my_POSIX)
str(my_Date)
data <- format(my_Date)
str(data)
my_DF <- data.frame(my_POSIX)
str(my_DF)
DF_Date <- as.Date(my_DF$my_POSIX)
str(DF_Date)
DF_Date
The consequence (for my LC_TIME and machine time zone) is that when
as.Date() is applied to the data frame column, it dispatches on
as.Date.POSIXct() not as.Date.POSIXlt(), causing a day shift (actually 60
minutes, but because as.Date.POSIXct() says floor(), it ends up being a
whole day). Should data.frame() be changing POSIXlt to POSIXct?
As as.data.frame.POSIXlt() is written, it says:
> as.data.frame.POSIXlt
function (x, row.names = NULL, optional = FALSE, ...)
{
value <- as.data.frame.POSIXct(as.POSIXct(x), row.names,
optional, ...)
if (!optional)
names(value) <- deparse(substitute(x))[[1]]
value
}
<environment: namespace:base>
which seems a little brutal. Using I() seems to be a work-around:
my_DF <- data.frame(my_POSIXa=I(my_POSIX))
str(my_DF)
class(my_DF$my_POSIXa)
DF_Date <- as.Date(my_DF$my_POSIXa)
str(DF_Date)
DF_Date
In the original problem (conversion of a list read from PostgreSQL to
a data frame with as.data.frame()), having to know that you need to insert
I() is perhaps unexpected.
Roger
--
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no
More information about the R-help
mailing list