[R] POSIXlt and trunc

Rui Barradas ruipbarradas at sapo.pt
Fri Jun 15 11:38:23 CEST 2012


Hello,

It works with me. Problem with R version? OS?

sessionInfo()
R version 2.15.0 (2012-03-30)
Platform: i386-pc-mingw32/i386 (32-bit)

locale:
[1] LC_COLLATE=Portuguese_Portugal.1252 
LC_CTYPE=Portuguese_Portugal.1252
[3] LC_MONETARY=Portuguese_Portugal.1252 LC_NUMERIC=C 

[5] LC_TIME=Portuguese_Portugal.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

loaded via a namespace (and not attached):
[1] tools_2.15.0

Hope this helps,

Rui Barradas

Em 15-06-2012 06:06, James Long escreveu:
> 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