Skip to content
Prev 312018 / 398503 Next

printing difftime summary

It looks like summary.data.frame(d) calls format(d[[i]]) for i in seq_len(ncol(d))
and pastes the results together into a "table" object for printing.  Hence, write
a format.summary.difftime if you want objects of class "summary.difftime" (which
I assume summary.difftime produces) to be formatted as you wish when a
difftime object is in a data.frame.  Once you've written it, have your print.summary.difftime
call it too.

E.g., with the following methods
summary.difftime <- function(x, ...) {
         ret <- quantile(x, p=(0:2)/2, na.rm=TRUE)
         class(ret) <- c("summary.difftime", class(ret))
         ret
}
format.summary.difftime <- function(x, ...) c(Min.Med.Max = paste(collapse="...", NextMethod("format")))
print.summary.difftime <- function(x, ...){ print(format(x), quote=FALSE) ; invisible(x) }

I get
Num         Date                    Delta
 Min.   :1   Min.   :2012-11-26   Min.Med.Max: 1 days... 4 days...16 days
 1st Qu.:2   1st Qu.:2012-11-27
 Median :3   Median :2012-11-28
 Mean   :3   Mean   :2012-11-28
 3rd Qu.:4   3rd Qu.:2012-11-29
 Max.   :5   Max.   :2012-11-30
Min.Med.Max
 1 days... 4 days...16 days

My summary.difftime inherits from difftime so the format method is not really
needed, as format.difftime does a reasonable job (except that it does not copy
the input names to its output).  I put it in to show how it gets called.


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com