[R] Adding 1 month to a dataframe column

ANGELO.LINARDI at bancaditalia.it ANGELO.LINARDI at bancaditalia.it
Thu Sep 18 18:24:45 CEST 2008


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. **



More information about the R-help mailing list