[R] Determine all specific same dates between two given dates

Uwe Ligges ligges at statistik.tu-dortmund.de
Sat Jul 26 16:07:46 CEST 2014

On 25.07.2014 18:26, Frank S. wrote:
> Hi everyone, After trying to find the solution during days, I decided to write in this help list in order to ask  if anyone can help me.I would want to construct an R function, with  "initial", "final" and "specific" dates as 3 arguments (for example, becauseI'm not really sure it is the best way), which returns a vector "v" containing all the specific same dates between the given "initial" and "final" dates (so the vector will contain only the specific date as many times as it appears inside the reference period). The function could have this aspect: myfun <- function(initial=as.Date("initial"), final=as.Date("final"),                             day_specific=day, month_specific=month)                                               { ....                     return(v)                    } Example of what I'm trying to explain: Let's say I want to obtain all the dates 25st September contained between initial="05-07-1990" and final="19-03-1998": myfun (initial=as.Date("05-07-1!
>   998"), final=as.Date("19-03-2003"),             day_specific=25, month_specific=9) The result I would expect is: v = c ("25-09-1998", "25-09-1999", "25-09-2000", "25-09-2001", "25-09-2002") Please, if somebody has any idea how to do the function, it will help me a lot. Thanks in advance!   		 	   		
> 	[[alternative HTML version deleted]]
> ______________________________________________
> 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.

Your mail is messed up (no HTML mails please!)

Many solutions, one may be:

myfun <- function(initial, final, day_specific, month_specific){
     v <- as.Date(paste(format(c(initial, final), "%Y"), month_specific, 
day_specific, sep="-"))
     v <- seq(v[1], v[2], by = "year")
     v[v >= initial & v <= final]

initial <- as.Date("05-07-1998", "%d-%m-%Y")
final <- as.Date("19-03-2003", "%d-%m-%Y")
day_specific <- 25
month_specific <- 9

myfun(initial, final, day_specific, month_specific)

More information about the R-help mailing list