[R] Calculation with date
Rui Barradas
ruipbarradas at sapo.pt
Sat Mar 9 13:24:58 CET 2013
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:
> Hello again,
>
> Let say I have an non-negative integer vector (which may be random):
>
> Vec <- c(0, 13, 10, 4)
>
> And I have a date:
>
>> Date <- as.Date(Sys.time())
>> Date
> [1] "2013-03-09"
>
>
>
> Using these 2 information, I want to get following date-vector:
>
> New_Vec <- c("2013-03-01", "2014-04-01", "2014-01-01", "2013-07-01")
>
> Basically the month-difference between 'New_Vec' and 'Date' will be 'Vec '
>
> Is there any R function to do it programmatically?
>
>
> Thanks for your help.
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list