[R] POSIXlt and trunc

R. Michael Weylandt michael.weylandt at gmail.com
Fri Jun 15 15:21:13 CEST 2012


General rule of thumb: use POSIXct rather than POSIXlt for such things
and only convert to POSIXlt (losslessly) at the end of your
calculations when you need it for display reasons. Internally, POSIXct
is just a double, so it is nice to do calculations on, while POSIXlt
is a list-like object which tries to handle the book-keeping of all
sorts of different time elements. See RNews 4/1 for more detail.

Not sure if this is immediately the problem at hand, but I've found it
to be good practice.

Best,
Michael

On Fri, Jun 15, 2012 at 12:06 AM, James Long <jpl2116 at gmail.com> wrote:
> 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]]
>
> ______________________________________________
> R-help at r-project.org 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.



More information about the R-help mailing list