[R] median time period
Iain Gallagher
iaingallagher at btopenworld.com
Wed Jun 29 17:50:31 CEST 2011
Typical - you post to the list and then work it out for yourself!
Anyway here's my solution
Toy code as before then:
intervalsMonths <- 12 * intervals$year + intervals$month
#convert whole years to months then add the remaining months for that entry in intervals
medianMonths <- median(as.numeric(intervalsMonths))
Best
iain
--- On Wed, 29/6/11, Iain Gallagher <iaingallagher at btopenworld.com> wrote:
> From: Iain Gallagher <iaingallagher at btopenworld.com>
> Subject: [R] median time period
> To: r-help at r-project.org
> Date: Wednesday, 29 June, 2011, 16:24
> 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
>
>
> ______________________________________________
> 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