Skip to content
Prev 319108 / 398503 Next

Calculation with date

Hello,

I don't believe there's such a function, but you can write one.

Date <- as.Date(Sys.time())
New_Vec <- c("2013-03-01", "2014-04-01", "2014-01-01", "2013-07-01")
New_Vec <- as.Date(New_Vec)
Vec <- c(0, 13, 10, 4)


plusmonths <- function(x, y){
	s <- as.integer(format(x, "%m")) + y
	yx <- as.integer(format(x, "%Y")) + (s %/% 12)
	as.Date(paste(yx, s %% 12, "01", sep = "-"))
}
pm <- plusmonths(Date, Vec)

identical(New_Vec, pm)  # TRUE


Hope this helps,

Rui Barradas

Em 09-03-2013 11:41, Christofer Bogaso escreveu: