[R] puzzled by math on date-time objects

William Dunlap wdunlap at tibco.com
Tue Mar 10 19:40:11 CET 2009


median.default was changed between 2.7.1 and 2.8.1 to
call sum(...)/2 instead of mean(...) and that causes
the problem for POSIXct objects (sum fails but mean
works for them).

Bill Dunlap
TIBCO Software Inc - Spotfire Division
wdunlap tibco.com  

> -----Original Message-----
> From: William Dunlap 
> Sent: Tuesday, March 10, 2009 11:37 AM
> To: 'r-help at r-project.org'
> Subject: Re: [R] puzzled by math on date-time objects
> 
> Your problem arises in R 2.8.1 (and 2.9.0-devel, but not 2.7.0) when
> length(POSIXct object) is even, because median(POSIXct object)
> passes a POSIXct object to median.default, which calls
> sum() in the even length case.
> 
> >  median( as.POSIXct(Sys.time()))
> [1] "2009-03-10 10:28:46 PDT"
> >  median( as.POSIXct(rep(Sys.time(),2)))
> Error in Summary.POSIXct(c(1236706132.54740, 
> 1236706132.54740), na.rm = FALSE) :
>   'sum' not defined for "POSIXt" objects
> > traceback()
> 4: stop(gettextf("'%s' not defined for \"POSIXt\" objects", .Generic),
>        domain = NA)
> 3: Summary.POSIXct(c(1236706132.54740, 1236706132.54740), 
> na.rm = FALSE)
> 2: median.default(as.POSIXct(rep(Sys.time(), 2)))
> 1: median(as.POSIXct(rep(Sys.time(), 2)))
> > version
>                _
> platform       i686-pc-linux-gnu
> arch           i686
> os             linux-gnu
> system         i686, linux-gnu
> status
> major          2
> minor          8.1
> year           2008
> month          12
> day            22
> svn rev        47281
> language       R
> version.string R version 2.8.1 (2008-12-22)
> 
> Bill Dunlap
> TIBCO Software Inc - Spotfire Division
> wdunlap tibco.com 
> 
> ------------------------------------------------------------
> [R] puzzled by math on date-time objects
> 
> Denis Chabot chabotd at globetrotter.net 
> Tue Mar 10 16:44:07 CET 2009
> Previous message: [R] nonmetric clustering
> Next message: [R] perform subgroup meta-analysis and create 
> forest plot	displaying subgroups
> Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
> Hi,
> 
> I don't understand the following. When I create a small 
> artificial set  
> of date information in class POSIXct, I can calculate the 
> mean and the  
> median:
> 
> a = as.POSIXct(Sys.time())
> a = a + 60*0:10; a
> 
>   [1] "2009-03-10 11:30:16 EDT" "2009-03-10 11:31:16 EDT" 
> "2009-03-10  
> 11:32:16 EDT"
>   [4] "2009-03-10 11:33:16 EDT" "2009-03-10 11:34:16 EDT" 
> "2009-03-10  
> 11:35:16 EDT"
>   [7] "2009-03-10 11:36:16 EDT" "2009-03-10 11:37:16 EDT" 
> "2009-03-10  
> 11:38:16 EDT"
> [10] "2009-03-10 11:39:16 EDT" "2009-03-10 11:40:16 EDT"
> 
> median(a)
> [1] "2009-03-10 11:35:16 EDT"
> mean(a)
> [1] "2009-03-10 11:35:16 EDT"
> 
> 
> But for real data (for this post, a short subset is in object 
> c)  that  
> I have converted into a POSIXct object, I cannot calculate 
> the median  
> with median(), though I do get it with summary():
> 
> c
>   [1] "2009-02-24 14:51:18 EST" "2009-02-24 14:51:19 EST" 
> "2009-02-24  
> 14:51:19 EST"
>   [4] "2009-02-24 14:51:20 EST" "2009-02-24 14:51:20 EST" 
> "2009-02-24  
> 14:51:21 EST"
>   [7] "2009-02-24 14:51:21 EST" "2009-02-24 14:51:22 EST" 
> "2009-02-24  
> 14:51:22 EST"
> [10] "2009-02-24 14:51:22 EST"
> 
> class(c)
> [1] "POSIXt"  "POSIXct"
> 
> median(c)
> Erreur dans Summary.POSIXct(c(1235505080.6, 1235505081.1), na.rm =  
> FALSE) :
>    'sum' not defined for "POSIXt" objects
> 
> One difference is that in my own date-time series, some events are  
> repeated (the original data contained fractions of seconds). 
> But then,  
> why can I get a median through summary()?
> 
> summary(c)
>                       Min.                   1st  
> Qu.                    Median
> "2009-02-24 14:51:18 EST" "2009-02-24 14:51:19 EST" "2009-02-24  
> 14:51:20 EST"
>                       Mean                   3rd  
> Qu.                      Max.
> "2009-02-24 14:51:20 EST" "2009-02-24 14:51:21 EST" "2009-02-24  
> 14:51:22 EST"
> 
> Thanks in advance,
> 
> 
> Denis Chabot
> 
> sessionInfo()
> R version 2.8.1 Patched (2009-01-19 r47650)
> i386-apple-darwin9.6.0
> 
> locale:
> fr_CA.UTF-8/fr_CA.UTF-8/C/C/fr_CA.UTF-8/fr_CA.UTF-8
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> other attached packages:
> [1] doBy_3.7     chron_2.3-30
> 
> loaded via a namespace (and not attached):
> [1] Hmisc_3.5-2     cluster_1.11.12 grid_2.8.1      lattice_0.17-20  
> tools_2.8.1
> 




More information about the R-help mailing list