[R] adding 1 month to a date
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Oct 12 20:27:42 CEST 2005
Try this. Note that mapply strips off the class which is why we
set up dd with the correct class and then just replaced the values.
# test data
d <- as.Date("2005-1-1") + seq(0,90,30)
# calculations, dd is the result
next.month <- function(x) seq(x, len = 2, by = "months")[2]
dd <- d
dd[] <- mapply(next.month, dd)
On 10/12/05, t c <quantpm at yahoo.com> wrote:
> Thanks. How do I use this to calculate a new variable (e.g."data$next_month") from an existing variable (e.g."Data$date_").
>
> I tried : <data$next_month<-seq(as.Date(data$date_), len = 2, by = "month")[2]>,
>
> but get the following error message: "Error in seq.Date(as.Date(data$date), len = 2, by = "1 month") : 'from' must be of length 1"
>
> Thanks.
>
>
>
> Gabor Grothendieck <ggrothendieck at gmail.com> wrote:
> Try this:
>
> seq(as.Date("2005-01-15"), len = 2, by = "month")[2]
>
> or here is another approach:
>
> http://finzi.psych.upenn.edu/R/Rhelp02a/archive/61570.html
>
> On 10/11/05, t c wrote:
> >
> > Within an R dataset, I have a date field called "date_". (The dates are in the format "YYYY-MM-DD", e.g. "1995-12-01".)
> >
> >
> >
> > How can I add or subtract "1 month" from this date, to get "1996-01-01" or " "1995-11-01".
> >
> >
> >
> >
> >
> > ---------------------------------
> >
> > [[alternative HTML version deleted]]
> >
> >
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> >
> >
>
>
> ---------------------------------
>
>
>
> ---------------------------------
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>
More information about the R-help
mailing list