[R] printing difftime summary

arun smartpink111 at yahoo.com
Wed Nov 21 23:04:36 CET 2012


Hello,

Just a doubt.  Are you looking for some other function (difftime2string) ot just remove the quotes from the printed output?

If it is the latter, then this should do it.
res<-do.call(data.frame,lapply(s,difftime2string))
 names(res)<-names(s)
 res
#       Min.   1st Qu.    Median     Mean  3rd Qu.      Max.
#1 500.00 ms 17.12 min 99.48 min 8.30 hrs 8.05 hrs 6.98 days

A.K.




----- Original Message -----
From: Sam Steingold <sds at gnu.org>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, November 21, 2012 2:22 PM
Subject: [R] printing difftime summary

Hi,
I have a vector of difftime objects and I want to see its summary.
Alas:
--8<---------------cut here---------------start------------->8---
> summary(infl$delay)
  Length    Class     Mode 
9008386 difftime  numeric 
--8<---------------cut here---------------end--------------->8---
this is almost completely useless.
I can use as.numeric:
--8<---------------cut here---------------start------------->8---
> s <- summary(as.numeric(infl$delay))
> dput(s)
structure(c(0.5, 1027, 5969, 29870, 28970, 603100), .Names = c("Min.", 
"1st Qu.", "Median", "Mean", "3rd Qu.", "Max."), class = c("summaryDefault", 
"table"))
> s
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
     0.5   1027.0   5969.0  29870.0  28970.0 603100.0 
--8<---------------cut here---------------end--------------->8---
but the printed representation is very unreadable: the fact that
603100.0 is almost exactly 7 days is not obvious.
Okay, maybe as.difftime will help?
--8<---------------cut here---------------start------------->8---
> as.difftime(s,units="secs")
Time differences in secs
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
     0.5   1027.0   5969.0  29870.0  28970.0 603100.0 
> as.difftime(s/3600,units="hours")
Time differences in hours
        Min.      1st Qu.       Median         Mean      3rd Qu.         Max. 
1.388889e-04 2.852778e-01 1.658056e+00 8.297222e+00 8.047222e+00 1.675278e+02 
--8<---------------cut here---------------end--------------->8---
nope; still unreadable.

What I really want to see _printed_ is something likes this:
--8<---------------cut here---------------start------------->8---
> sapply(s,difftime2string)
       Min.     1st Qu.      Median        Mean     3rd Qu.        Max. 
"500.00 ms" "17.12 min" "99.48 min"  "8.30 hrs"  "8.05 hrs" "6.98 days" 
--8<---------------cut here---------------end--------------->8---
except that the quotes are not needed in the printed output.
Here I wrote:
--8<---------------cut here---------------start------------->8---
difftime2string <- function (x) {
  if (x < 1) return(sprintf("%.2f ms",x*1000))
  if (x < 100) return(sprintf("%.2f sec",x))
  if (x < 6000) return(sprintf("%.2f min",x/60))
  if (x < 108000) return(sprintf("%.2f hrs",x/3600))
  if (x < 400*24*3600) return(sprintf("%.2f days",x/(24*3600)))
  sprintf("%.2f years",x/(365.25*24*3600))
}
--8<---------------cut here---------------end--------------->8---

So, what is "The Right R Way" to print a summary of difftime objects?
Thanks!
-- 
Sam Steingold (http://sds.podval.org/) on Ubuntu 12.04 (precise) X 11.0.11103000
http://www.childpsy.net/ http://openvotingconsortium.org
http://memri.org http://camera.org http://mideasttruth.com http://pmw.org.il
MS Windows: error: the operation completed successfully.

______________________________________________
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