[R] printing difftime summary

David Winsemius dwinsemius at comcast.net
Sun Nov 25 09:50:51 CET 2012


On Nov 24, 2012, at 7:48 PM, Sam Steingold wrote:

>> * David Winsemius <qjvafrzvhf at pbzpnfg.arg> [2012-11-23 13:14:17  
>> -0800]:
>>
>>>> See http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-should-I-write-summary-methods_003f
>
> --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("data.frame","summary.difftime")
>  r
> }
> print.summary.difftime <- function (sd) print.data.frame(sd)
> --8<---------------cut here---------------end--------------->8---
>
> it appears to work for a single vector:
>
> --8<---------------cut here---------------start------------->8---
>> r1 <- summary(infl$delay)
>> r1
>           string     secs
> Min.    492.00 ms      0.5
> 1st Qu. 18.08 min   1085.0
> Median   1.77 hrs   6370.0
> Mean     8.20 hrs  29530.0
> 3rd Qu.  8.12 hrs  29250.0
> Max.    6.98 days 602900.0
>> str(r1)
> Classes 'summary.difftime' and 'data.frame':	6 obs. of  2 variables:
> $ string: chr  "492.00 ms" "18.08 min" "1.77 hrs" "8.20 hrs" ...
> $ secs  :Classes 'summaryDefault', 'table'  num [1:6] 4.92e-01 1.08e 
> +03 6.37e+03 2.95e+04 2.92e+04 ...
> --8<---------------cut here---------------end--------------->8---
>
> but not as a part of data frame:
>
> --8<---------------cut here---------------start------------->8---
>> a <- summary(infl)
> Error in summary.difftime(X[[22L]], ...) :
>  unused argument(s) (maxsum = 7, digits = 12)
> --8<---------------cut here---------------end--------------->8---
>
> I guess I should somehow accept a list of options in  
> summary.difftime()
> and pass them on to the inner call to summary() (or should it be
> explicitly summary.numeric()?)
>

In the usual way. If you know that the function will be called with  
arguments from the summary.data.frame function then you should allow  
the argument list to accept them. You can ignore them or provide  
provisions for them. You just can't define your function to have only  
one argument if you expect (as you should since you passes summary a  
dataframe object) that it might be called within summary.data.frame.

This is the argument list for summary.data.frame:

 >   summary.data.frame
function (object, maxsum = 7, digits = max(3, getOption("digits") -
     3), ...)

> how do I do that?

summary.difftime <- function (v, ... ) { ................

There are many asked and answered questions on rhelp about how to deal  
with the "dots" arguments.

-- 
David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list