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? Thank for your help [[alternative HTML version deleted]]
Use, format() to extract a character string representation of the year, then paste() it together with Months. Like this: paste(Months, format(Given_Date, format = "%Y"), sep = "-") See ?strftime for details. Best, Ista On Tue, Dec 31, 2013 at 4:53 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? > > Thank for your help > > [[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.
On 01/01/2014 08:53 AM, Christofer Bogaso 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? >Hi Christofer, I would like to be an accomplice in this, but I can't quite work out your logic. I thought that you might want: IF Given_Date is in the same month as an element of Months THEN use the year in Given_Date ELSE use the year in Given_Date plus 1 However, when I programmed it: assignYear<-function(index_date,months) { index_year<-as.numeric(format(index_date,"%Y")) index_month<-which(months==format(index_date,"%b")) dates<-as.Date(paste(index_year+1,months,"1",sep="-"),"%Y-%b-%d") dates[index_month]<- as.Date(paste(index_year,months[index_month],"1",sep="-"),"%Y-%b-%d") return(format(dates,"%Y-%b")) } it didn't work for the second example. Could you give some idea of what you want to do? Jim
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? Thank for your help ??? [[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.