Hi,

I'm having trouble understanding how trunc is operating on vectors of
POSIXlt objects. Why does dates[1:4] in the last line return a bunch of NAs
even though dates look like it has all the right elements? This worries me
that something is off with my use of trunc. Is trunc not suppose to be
vectorized with POSIXlt? If not, then how should I truncate a bunch of
POSIXlt objects? I'm using R 2.11.1 with linux. (easy cut and paste R code
below)


> dates <- c("2011-12-30 20:03:18 PST","2012-01-08 19:10:00 PST",
+             "2012-01-15 22:00:10 PST","2012-01-26 20:01:00 PST",
+             "2012-01-27 21:25:01 PST","2012-01-19 20:17:31 PST",
+             "2012-01-31 21:01:00 PST","2012-01-15 21:01:00 PST",
+             "2012-01-04 21:03:00 PST","2012-01-04 20:00:00 PST")
> dates = as.POSIXlt(dates)
> dates[1:4]
[1] "2011-12-30 20:03:18" "2012-01-08 19:10:00" "2012-01-15 22:00:10"
[4] "2012-01-26 20:01:00"
> class(dates)
[1] "POSIXt"  "POSIXlt"
> ### this all looks normal so far, now for truncating
> dates = trunc(dates,units="hours")
> class(dates)
[1] "POSIXt"  "POSIXlt"
> dates
 [1] "2011-12-30 20:00:00" "2012-01-08 19:00:00" "2012-01-15 22:00:00"
 [4] "2012-01-26 20:00:00" "2012-01-27 21:00:00" "2012-01-19 20:00:00"
 [7] "2012-01-31 21:00:00" "2012-01-15 21:00:00" "2012-01-04 21:00:00"
[10] "2012-01-04 20:00:00"
> dates[1:4]
[1] "2011-12-30 20:00:00" NA                    NA
[4] NA
> # what?


Easy cut and paste code:

dates <- c("2011-12-30 20:03:18 PST","2012-01-08 19:10:00 PST",
            "2012-01-15 22:00:10 PST","2012-01-26 20:01:00 PST",
            "2012-01-27 21:25:01 PST","2012-01-19 20:17:31 PST",
            "2012-01-31 21:01:00 PST","2012-01-15 21:01:00 PST",
            "2012-01-04 21:03:00 PST","2012-01-04 20:00:00 PST")
dates = as.POSIXlt(dates)
dates[1:4]
class(dates)
dates = trunc(dates,units="hours")
class(dates)
dates
dates[1:4]


Thanks for your help!
James

	[[alternative HTML version deleted]]

