[R] printing difftime summary
William Dunlap
wdunlap at tibco.com
Tue Nov 27 00:02:48 CET 2012
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
> d <- data.frame(Num=1:5, Date=as.Date("2012-11-26")+(0:4), Delta=diff(as.Date("2012-11-26")+2^(0:5)))
> summary(d)
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
> summary(d$Delta)
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
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Sam Steingold
> Sent: Monday, November 26, 2012 2:20 PM
> To: r-help at r-project.org; David Winsemius
> Subject: Re: [R] printing difftime summary
>
> > * David Winsemius <qjvafrzvhf at pbzpnfg.arg> [2012-11-26 08:46:35 -0800]:
> >
> > On Nov 26, 2012, at 7:14 AM, Sam Steingold wrote:
> >
> >> summary(infl), where infl$delay is a difftime vector, prints
> >>
> >> ...
> >>
> >> delay
> >> string:c("492.00 ms", "18.08 min", "1.77 hrs", "8.20 hrs", "8.13 hrs",
> >> "6.98 days")
> >> secs :c(" 0.5", " 1085.1", " 6370.2", " 29534.4", " 29254.0",
> >> "602949.7")
> >>
> >>
> >>
> >> instead of something like
> >>
> >> delay
> >> Min.: 492 ms
> >> 1st Qu.: 18.08 min
> >>
> >> &c
> >>
> >> so, how do I arrange for a proper printing of difftime summary as a
> >> part
> >> of the data frame summary?
> >
> > If you like a particular format from an existing print method then why
> > not look it up and copy the code?
> >
> > methods(print)
>
> the problem is that I cannot figure out which function prints this:
>
> >> delay
> >> string:c("492.00 ms", "18.08 min", "1.77 hrs", "8.20 hrs", "8.13 hrs",
> >> "6.98 days")
> >> secs :c(" 0.5", " 1085.1", " 6370.2", " 29534.4", " 29254.0",
> >> "602949.7")
>
> I added cat()s to print.summary.difftime and I do not see them, so it
> appears that I have no direct control over how a summary.difftime is
> printed as a part of a summary of a data.frame.
>
>
> --8<---------------cut here---------------start------------->8---
> summary.difftime <- function (v, ...) {
> s <- summary(as.numeric(v), ...)
> r <- as.data.frame(sapply(s,difftime2string),stringsAsFactors=FALSE)
> names(r) <- c("string")
> r[[units(v)]] <- s
> class(r) <- c("summary.difftime","data.frame")
> invisible(r)
> }
> print.summary.difftime <- function (sd, ...) {
> cat("[[[print.summary.difftime]]]\n")
> print(list(...))
> print.data.frame(sd, ...)
> }
> --8<---------------cut here---------------end--------------->8---
>
> --
> Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
> http://www.childpsy.net/ http://palestinefacts.org http://think-israel.org
> http://www.memritv.org http://openvotingconsortium.org http://mideasttruth.com
> The force of gravity doubles when acting on a body on a couch.
>
> ______________________________________________
> 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