[R] puzzled by math on date-time objects

Denis Chabot chabotd at globetrotter.net
Tue Mar 10 16:44:07 CET 2009


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