[R] Adding 1 month to a dataframe column
Gabor Grothendieck
ggrothendieck at gmail.com
Thu Sep 18 21:15:59 CEST 2008
Try this:
# example data - Jan 31/00, Feb 29/00, Mar 31/00, ..., Dec 31/00
d <- seq(as.Date("2000-02-01"), by = "month", length = 12) - 1
d
# find beginning of next month
nextmon <- function(d) as.Date(cut(as.Date(cut(d, "month")) + 32, "month"))
# add number of days from start of current month to next month
nextmon(d) + as.POSIXlt(d)$mday - 1
# If all your dates are at month end and you want the plus 1 month
# to be at month end too then replace last line with:
nextmon(nextmon(d)) - 1
The last one can be done even more easily with as.yearmon from zoo.
It converts the dates to a year plus 0 for jan, 1/12 for feb, etc. Also,
as.Date.yearmon(..., frac = 1) converts back from yearmon class to
Date class using end of month for the result so:
library(zoo)
as.Date(as.yearmon(d) + 1/12, frac = 1)
On Thu, Sep 18, 2008 at 12:24 PM, <ANGELO.LINARDI at bancaditalia.it> wrote:
> Dear R experts,
> I have a problem in modifying one column of a dataframe with a datatime
> format using a datetime operator.
> Here is my dataframe A:
>
> DATACONT PROVINCIA VALORE
> 1 2007-12-31 MI 1
> 2 2007-12-31 PV 2
> 3 2007-12-31 NA 3
> 4 2007-12-31 MI 4
> 5 2007-12-31 RM 5
> 6 2007-12-31 RM 6
> 7 2007-12-31 MI 7
> 8 2008-12-31 MI 11
> 9 2008-12-31 PV 12
> 10 2008-12-31 NA 13
> 11 2008-12-31 MI 14
> 12 2008-12-31 RM 15
> 13 2008-12-31 RM 16
> 14 2008-12-31 MI 17
> 15 2006-12-31 MI -9
> 16 2006-12-31 PV -8
> 17 2006-12-31 NA -7
> 18 2006-12-31 MI -6
> 19 2006-12-31 RM -5
> 20 2006-12-31 RM -4
> 21 2006-12-31 MI -3
>
> Suppose I want to shift all dates by 1 year, obtaining the following
> dataframe B:
>
> DATACONT PROVINCIA VALORE
> 1 2008-12-31 MI 1
> 2 2008-12-31 PV 2
> 3 2008-12-31 NA 3
> 4 2008-12-31 MI 4
> 5 2008-12-31 RM 5
> 6 2008-12-31 RM 6
> 7 2008-12-31 MI 7
> 8 2009-12-31 MI 11
> 9 2009-12-31 PV 12
> 10 2009-12-31 NA 13
> 11 2009-12-31 MI 14
> 12 2009-12-31 RM 15
> 13 2009-12-31 RM 16
> 14 2009-12-31 MI 17
> 15 2007-12-31 MI -9
> 16 2007-12-31 PV -8
> 17 2007-12-31 NA -7
> 18 2007-12-31 MI -6
> 19 2007-12-31 RM -5
> 20 2007-12-31 RM -4
> 21 2007-12-31 MI -3
>
> I tried with seq.Date:
> seq.Date(a$"DATA",by="1 month",len=2)[2]
>
> but it works only for single values (the "from" must have length=1).
> Any hints ?
> Thank you in advance
>
> Angelo Linardi
>
> ** Le e-mail provenienti dalla Banca d'Italia sono trasmesse in buona fede e non
> comportano alcun vincolo ne' creano obblighi per la Banca stessa, salvo che cio' non
> sia espressamente previsto da un accordo scritto.
> Questa e-mail e' confidenziale. Qualora l'avesse ricevuta per errore, La preghiamo di
> comunicarne via e-mail la ricezione al mittente e di distruggerne il contenuto. La
> informiamo inoltre che l'utilizzo non autorizzato del messaggio o dei suoi allegati
> potrebbe costituire reato. Grazie per la collaborazione.
> -- E-mails from the Bank of Italy are sent in good faith but they are neither binding on
> the Bank nor to be understood as creating any obligation on its part except where
> provided for in a written agreement. This e-mail is confidential. If you have received it
> by mistake, please inform the sender by reply e-mail and delete it from your system.
> Please also note that the unauthorized disclosure or use of the message or any
> attachments could be an offence. Thank you for your cooperation. **
>
> ______________________________________________
> 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