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]]
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. >
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.
Reasonably Related Threads
- round.Date and trunc.Date not working / implemented
- round.Date and trunc.Date not working / implemented
- {round,trunc}.POSIXt and daylight savings time (PR#1543)
- round.Date and trunc.Date not working / implemented
- Error with cut.POSIXt and daylight savings time switchover dates