[R] printing difftime summary

Sam Steingold sds at gnu.org
Mon Nov 26 16:14:49 CET 2012


this overcomes the summary generation, but not printing:

--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---

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?

> * David Winsemius <qjvafrzvhf at pbzpnfg.arg> [2012-11-25 00:50:51 -0800]:
>
> 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.

-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://www.memritv.org http://memri.org
http://honestreporting.com http://dhimmi.com http://openvotingconsortium.org
People with a good taste are especially appreciated by cannibals.




More information about the R-help mailing list