[R] median time period

Iain Gallagher iaingallagher at btopenworld.com
Wed Jun 29 17:24:55 CEST 2011


Hello List

I'm trying to calculate the median period (in months) of a set of time intervals (between two interventions). 

I have been playing with the lubridate package to create the intervals but I can't think of the right approach to get the median timeperiod.

Toy code:

library(lubridate)
test <- c('08-04-22', '08-07-28', '09-03-02', '09-03-03', '09-01-30', '09-03-09', '10-02-24', '10-03-05')
test <- ymd(test)
intervals <- as.period(test[5:8] - test[1:4])

intervals
[1] 9 months and 8 days    7 months and 9 days    11 months and 22 days 
[4] 1 year and 2 days 

How can I convert this 'period' object to months? From there I think I should just convert to 'numeric' and calculate the median.

Garrett if you're out there - great package but could you help please!?

Best

iain

> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)

locale:
 [1] LC_CTYPE=en_GB.utf8       LC_NUMERIC=C             
 [3] LC_TIME=en_GB.utf8        LC_COLLATE=en_GB.utf8    
 [5] LC_MONETARY=C             LC_MESSAGES=en_GB.utf8   
 [7] LC_PAPER=en_GB.utf8       LC_NAME=C                
 [9] LC_ADDRESS=C              LC_TELEPHONE=C           
[11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C      

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lubridate_0.2.4

loaded via a namespace (and not attached):
[1] plyr_1.5.2  stringr_0.4




More information about the R-help mailing list