# [R] Working with Date

arun smartpink111 at yahoo.com
Wed Jan 1 00:33:41 CET 2014

```Hi,
May be this helps:

fun1 <- function(months, Given_date){
g1 <- format(Given_date, "%b")
indx1 <- match(months,month.abb)
indx2 <- match(g1, month.abb)
yr <- as.numeric(format(Given_date,"%Y"))
if(any(indx1 < indx2)){
ifelse(indx1 < indx2, paste(months, yr,sep="-"), paste(months, yr+1,sep="-"))
}

else{
paste(months,yr,sep="-")
}
}

Given_Date2 <- as.Date("2014-01-04")
fun1(Months,Given_Date)
#[1] "Jan-2013" "Dec-2014" "Mar-2013"
fun1(Months,Given_Date2)
#[1] "Jan-2014" "Dec-2014" "Mar-2014"
A.K.

On Tuesday, December 31, 2013 4:55 PM, Christofer Bogaso <bogaso.christofer at gmail.com> wrote:
Hi again,

Happy new year 2014 to every R gurus and users.

I am struggling with some calculation with dates... Let say I have
following vector of months:

Months <- c("Jan", "Dec", "Mar")

Now I need to assign year with them. This assignment will be based on some
given date. Let say my given date is :

Given_Date <- as.Date("2013-12-23")

So in this case, the modified month will be:

Months_Mod <- c("Jan-2014", "Dec-2013", "Mar-2014")

However if given date is:

Given_Date <- as.Date("2014-01-04")

then the modified months will be:

Months_Mod <- c("Jan-2014", "Dec-2014", "Mar-2014")

My problem is that, I can not extablish some logic around it, so that I can
do it programmatically for any Month-vector and for any Given-date.

Can someone help me to accomplice this?

[[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help