Bug 14679 - trunc.POSIXt (units != "secs") produces NAs when printing vectors
trunc.POSIXt (units != "secs") produces NAs when printing vectors
Status: CLOSED FIXED
Product: R
Classification: Unclassified
Component: Low-level
R 2.13.1 patched
x86_64/x64/amd64 (64-bit) Windows 64-bit
: P5 normal
Assigned To: R-core
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2011-09-19 17:17 UTC by Sam Buttrey
Modified: 2011-09-23 16:54 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sam Buttrey 2011-09-19 17:17:02 UTC
Hi. I've seen this reported elsewhere, but I didn't find it in Bugzilla.

trunc.POSIXt (units="days") acts on a vector of class POSIXlt by setting the sec, min, and hour components to a single zero. This confuses other operations. When you print the whole vector, all is well, but when printing subsets, NA's are produced.

Consider:
#
# Create some POSIXlt times
#
> my.times <- as.POSIXlt (c("2010-02-27 22:30:33", "2009-08-09 06:01:03",  "2010-07-23 17:29:59"))
#
# View the second one; it's okay.
#
> my.times[2]
[1] "2009-08-09 06:01:03"
#
# Truncate the times to the next-lower day
#
> trunc (my.times, units="days")
[1] "2010-02-27" "2009-08-09" "2010-07-23"
#
# Display all three -- now the second and third print as NA.
#
> trunc (my.times, units="days")[1:3]
[1] "2010-02-27" NA           NA
# I believe the problem is that the truncation (units = "days") replaces all 
# instances of sec, min, hour and isdst with a single scalar. Using a vector
# would fix this printing problem. One might, e.g., replace lines like
# x$sec <- 0
# with lines like
# x$sec[] <- 0 .
# Let's try that out. Build a new trunc.POSIXt; call it tP; make it text.
#
tP <- deparse (trunc.POSIXt)
#
# Replace lines like "x$sec <- 0" with lines like "x$sec[] <- 0"; likewise  -1
#
> tP <- gsub (" <- 0", "\\[\\] <- 0", tP)
> tP <- gsub (" <- -1", "\\[\\] <- -1", tP)
> tp <- eval (parse (text=tP)) # make back into function
#
# And now all is well.
#
> tp (my.times, units="days")[1:3]
[1] "2010-02-27" "2009-08-09" "2010-07-23"

Thanks,
Sam Buttrey
Comment 1 Brian Ripley 2011-09-23 16:54:58 UTC
Changed in 2.13.2RC