[R] Advice requested: Best method of coding "time since" repeating event

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 21 05:36:58 CEST 2008


Without some data we can't tell precisely the setup but here it is
using Date class
and you can adapt it to your specific situation.  We first
define functions to get the prior wed (or the same day if its wed) and the last
day of the prior month.  Then we define the test data e of event dates and
do the calculation.

library(zoo) # as.yearmon, as.Date.numeric

prev.wed <- function(x) 7 * floor(as.numeric(x - 3 + 4)/7) + as.Date(3 - 4)

prior.month.end <- function(x) as.Date(as.yearmon(x)) - 1

# test data
e <- Sys.Date() + seq(0, length = 3, by = 100)

as.numeric(e - prev.wed(prior.month.end(e))) * 24 * 60


On Wed, Aug 20, 2008 at 10:26 PM, M-J S Milloy <mjsmilloy at me.com> wrote:
>
> Hello,
>
> I have a dataset containing approx 1000 events spanning four years (2004.03 to 2008.07). For each event, I'd like to determine the time (in minutes) since the most recent final Wednesday of the month. I've found no obvious solution on the list or online; I think I'll try to use functions in the chron package.
>
> Anyone have any other advice?
>
> Thanks.
>
>
> M-J
>
>
> School of Population and Public Health,
> University of British Columbia
> Vancouver, Canada
>
> Centre for Excellence in HIV/AIDS,
> St. Paul's Hospital
> Vancouver, Canada
>
> ______________________________________________
> 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