Omar Lakkis
2005-Feb-15 16:59 UTC
[R] how many 7th of the month is there between two dates
This is a eaeir way to ask my prior question: I want to caculate how many an exact day of the month there is between two dates. For example; How many 7th of the month is there between "1998/12/17" and "2000/1/7". To make the problem simple, the day of the month (7) is the day in the 2nd date.
Gabor Grothendieck
2005-Feb-15 17:16 UTC
[R] how many 7th of the month is there between two dates
Omar Lakkis <uofiowa <at> gmail.com> writes: : I want to caculate how many an exact day of the month there is between : two dates. : : For example; How many 7th of the month is there between "1998/12/17" : and "2000/1/7". To make the problem simple, the day of the month (7) : is the day in the 2nd date. d1 <- as.Date("1998/12/17") d2 <- as.Date("2000/1/7") day <- function(x) as.numeric(format(x, "%d")) sum(day(seq(from = d1, to = d2, by = "day")) == day(d2))
Prof Brian Ripley
2005-Feb-15 18:04 UTC
[R] how many 7th of the month is there between two dates
It is really easy using R's date and datetime classes: st <- as.Date("1998-12-17") en <- as.Date("2000-1-7") st0 <- st # Get the 7th of the month before st0 <- as.POSIXlt(st); st0$mday <- 7; st0$mon <- st0$mon - 1 st0 <- as.Date(st0) ll <- seq.Date(st0, en, by="month") sum(ll > as.Date(st) & ll < en) and use >= etc depending what you mean by `between'. On Tue, 15 Feb 2005, Omar Lakkis wrote:> This is a eaeir way to ask my prior question: > > I want to caculate how many an exact day of the month there is between > two dates. > > For example; How many 7th of the month is there between "1998/12/17" > and "2000/1/7". To make the problem simple, the day of the month (7) > is the day in the 2nd date.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595