printing difftime summary
Looks like format.summary.difftime <- function(sd, ...) structure(sd$string, names=rownames(sd)) does the job. any reason not to use it?
On Mon, Nov 26, 2012 at 7:36 PM, William Dunlap <wdunlap at tibco.com> wrote:
why do I see NULLs?!
because
... format.difftime does a reasonable job (except that it does not copy the input names to its output).
Replace your call of the form format(difftimeObject) with structure(format(difftimeObject), names=names(difftimeObject)) to work around this. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com
-----Original Message-----
From: Sam Steingold [mailto:sam.steingold at gmail.com] On Behalf Of Sam Steingold
Sent: Monday, November 26, 2012 4:09 PM
To: William Dunlap
Cc: r-help at r-project.org; David Winsemius
Subject: Re: [R] printing difftime summary
Thanks a lot - almost there!
--8<---------------cut here---------------start------------->8---
format.summary.difftime <- function(sd, ...) {
t <- matrix(sd$string)
rownames(t) <- rownames(sd)
print(t)
format(as.table(t))
}
print.summary.difftime <- function (sd, ...) {
print(format(sd), quote=FALSE)
invisible(sd)
}
--8<---------------cut here---------------end--------------->8---
this almost works:
--8<---------------cut here---------------start------------->8---
summary(delays)
share.id min max 12cf12372b87cce9: 1 NULL:492.00 ms NULL:492.00 ms 12cf36060bdb9581: 1 NULL:3.70 min NULL:21.80 min 12d2665c906bb232: 1 NULL:20.32 min NULL:3.26 hrs 12d2802f1435b4cd: 1 NULL:5.52 hrs NULL:13.78 hrs 12d292988f5f8422: 1 NULL:2.81 hrs NULL:16.20 hrs 12d29dd2894e2790: 1 NULL:6.95 days NULL:6.98 days --8<---------------cut here---------------end--------------->8--- why do I see NULLs?! --8<---------------cut here---------------start------------->8---
t <- matrix(sd$string) rownames(t) <- rownames(sd) t
[,1] Min. "492.00 ms" 1st Qu. "3.70 min" Median "20.32 min" Mean "5.52 hrs" 3rd Qu. "2.81 hrs" Max. "6.95 days"
as.table(t)
A Min. 492.00 ms 1st Qu. 3.70 min Median 20.32 min Mean 5.52 hrs 3rd Qu. 2.81 hrs Max. 6.95 days
format(as.table(t))
A Min. "492.00 ms" 1st Qu. "3.70 min " Median "20.32 min" Mean "5.52 hrs " 3rd Qu. "2.81 hrs " Max. "6.95 days"
--8<---------------cut here---------------end--------------->8---
* William Dunlap <jqhaync at gvopb.pbz> [2012-11-26 23:02:48 +0000]: 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.
-- Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000 http://www.childpsy.net/ http://openvotingconsortium.org http://ffii.org http://www.memritv.org http://americancensorship.org There are two ways to write error-free programs; only the third one works.
Sam Steingold <http://sds.podval.org> <http://www.childpsy.net/>