Re: [R] adding 1 month to a date
Simple addition and subtraction works as well: as.Date(1995/12/01,format=%Y/%m/%d) + 30 If you have datetime values you can use strptime(1995-12-01 08:00:00,format=%Y-%m-%d %H:%M:%S) + 30*24*3600 where 30*24*3600 = 30 days expressed in seconds. -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 11, 2005 10:16 PM To: t c Cc: r-help@stat.math.ethz.ch Subject: Re: [R] adding 1 month to a date On Tue, 2005-10-11 at 16:26 -0700, t c wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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. There might be an easier way to do this, but using seq.Date(), you can increment or decrement from a Time 0 by months: Add 1 month: This takes your Time 0, generates a 2 element sequence (which begins with Time 0) and then takes the second element: seq(as.Date(1995-12-01), by = month, length = 2)[2] [1] 1996-01-01 Subtract 1 month: Same as above, but we use 'by = -1 month' and take the second element: seq(as.Date(1995-12-01), by = -1 month, length = 2)[2] [1] 1995-11-01 See ?as.Date and ?seq.Date for more information. The former function is used to convert from a character vector to a Date class object. Note that in your case, the date format is consistent with the default. Pay attention to the 'format' argument in as.Date() if your dates should be in other formats. HTH, Marc Schwartz __ R-help@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 __ R-help@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
Re: [R] adding 1 month to a date
On Wed, 12 Oct 2005, bogdan romocea wrote: Simple addition and subtraction works as well: as.Date(1995/12/01,format=%Y/%m/%d) + 30 If you have datetime values you can use strptime(1995-12-01 08:00:00,format=%Y-%m-%d %H:%M:%S) + 30*24*3600 where 30*24*3600 = 30 days expressed in seconds. Sorry, not in general, as a month is not generally of 30 days (including in your example). seq.Date is a good way to do this. -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 11, 2005 10:16 PM To: t c Cc: r-help@stat.math.ethz.ch Subject: Re: [R] adding 1 month to a date On Tue, 2005-10-11 at 16:26 -0700, t c wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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. There might be an easier way to do this, but using seq.Date(), you can increment or decrement from a Time 0 by months: Add 1 month: This takes your Time 0, generates a 2 element sequence (which begins with Time 0) and then takes the second element: seq(as.Date(1995-12-01), by = month, length = 2)[2] [1] 1996-01-01 Subtract 1 month: Same as above, but we use 'by = -1 month' and take the second element: seq(as.Date(1995-12-01), by = -1 month, length = 2)[2] [1] 1995-11-01 See ?as.Date and ?seq.Date for more information. The former function is used to convert from a character vector to a Date class object. Note that in your case, the date format is consistent with the default. Pay attention to the 'format' argument in as.Date() if your dates should be in other formats. HTH, Marc Schwartz __ R-help@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 __ R-help@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 -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ R-help@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
Re: [R] adding 1 month to a date
Thanks to Prof. Ripley for pointing this out. One of the approaches that I had considered here was to set up a vector of the number of days in each month (adjusting of course for leap years), and use day arithmetic to add/subtract the appropriate number of days. However, it was easier to use seq.Date() and to further consider putting a wrapper around it to make it yet even easier to use. Marc On Wed, 2005-10-12 at 13:23 +0100, Prof Brian Ripley wrote: On Wed, 12 Oct 2005, bogdan romocea wrote: Simple addition and subtraction works as well: as.Date(1995/12/01,format=%Y/%m/%d) + 30 If you have datetime values you can use strptime(1995-12-01 08:00:00,format=%Y-%m-%d %H:%M:%S) + 30*24*3600 where 30*24*3600 = 30 days expressed in seconds. Sorry, not in general, as a month is not generally of 30 days (including in your example). seq.Date is a good way to do this. -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 11, 2005 10:16 PM To: t c Cc: r-help@stat.math.ethz.ch Subject: Re: [R] adding 1 month to a date On Tue, 2005-10-11 at 16:26 -0700, t c wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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. There might be an easier way to do this, but using seq.Date(), you can increment or decrement from a Time 0 by months: Add 1 month: This takes your Time 0, generates a 2 element sequence (which begins with Time 0) and then takes the second element: seq(as.Date(1995-12-01), by = month, length = 2)[2] [1] 1996-01-01 Subtract 1 month: Same as above, but we use 'by = -1 month' and take the second element: seq(as.Date(1995-12-01), by = -1 month, length = 2)[2] [1] 1995-11-01 See ?as.Date and ?seq.Date for more information. The former function is used to convert from a character vector to a Date class object. Note that in your case, the date format is consistent with the default. Pay attention to the 'format' argument in as.Date() if your dates should be in other formats. HTH, Marc Schwartz __ R-help@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
Re: [R] adding 1 month to a date
OTOH, seq(as.Date(2004-01-31), by = month, length = 14) [1] 2004-01-31 2004-03-02 2004-03-31 2004-05-01 2004-05-31 [6] 2004-07-01 2004-07-31 2004-08-31 2004-10-01 2004-10-31 [11] 2004-12-01 2004-12-31 2005-01-31 2005-03-03 I would prefer to see dates forced to be within each month, not leaking into next month. IOW: [1] 2004-01-31 2004-02-29 2004-03-31 2004-04-30 2004-05-31, etc -- Jim Porzak Loyalty Matrix Inc. San Francisco, CA On 10/12/05, Marc Schwartz [EMAIL PROTECTED] wrote: Thanks to Prof. Ripley for pointing this out. One of the approaches that I had considered here was to set up a vector of the number of days in each month (adjusting of course for leap years), and use day arithmetic to add/subtract the appropriate number of days. However, it was easier to use seq.Date() and to further consider putting a wrapper around it to make it yet even easier to use. Marc On Wed, 2005-10-12 at 13:23 +0100, Prof Brian Ripley wrote: On Wed, 12 Oct 2005, bogdan romocea wrote: Simple addition and subtraction works as well: as.Date(1995/12/01,format=%Y/%m/%d) + 30 If you have datetime values you can use strptime(1995-12-01 08:00:00,format=%Y-%m-%d %H:%M:%S) + 30*24*3600 where 30*24*3600 = 30 days expressed in seconds. Sorry, not in general, as a month is not generally of 30 days (including in your example). seq.Date is a good way to do this. -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 11, 2005 10:16 PM To: t c Cc: r-help@stat.math.ethz.ch Subject: Re: [R] adding 1 month to a date On Tue, 2005-10-11 at 16:26 -0700, t c wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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. There might be an easier way to do this, but using seq.Date(), you can increment or decrement from a Time 0 by months: Add 1 month: This takes your Time 0, generates a 2 element sequence (which begins with Time 0) and then takes the second element: seq(as.Date(1995-12-01), by = month, length = 2)[2] [1] 1996-01-01 Subtract 1 month: Same as above, but we use 'by = -1 month' and take the second element: seq(as.Date(1995-12-01), by = -1 month, length = 2)[2] [1] 1995-11-01 See ?as.Date and ?seq.Date for more information. The former function is used to convert from a character vector to a Date class object. Note that in your case, the date format is consistent with the default. Pay attention to the 'format' argument in as.Date() if your dates should be in other formats. HTH, Marc Schwartz __ R-help@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 __ R-help@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
Re: [R] adding 1 month to a date
On Wed, 12 Oct 2005, Jim Porzak wrote: OTOH, seq(as.Date(2004-01-31), by = month, length = 14) [1] 2004-01-31 2004-03-02 2004-03-31 2004-05-01 2004-05-31 [6] 2004-07-01 2004-07-31 2004-08-31 2004-10-01 2004-10-31 [11] 2004-12-01 2004-12-31 2005-01-31 2005-03-03 I would prefer to see dates forced to be within each month, not leaking into next month. IOW: [1] 2004-01-31 2004-02-29 2004-03-31 2004-04-30 2004-05-31, etc It depends how you intend 1 month after 2004-01-31. Is the the same number of days before the beginning of the next month or after the end of the stated month? seq(as.Date(2004-02-01), by = month, length = 14)-1 [1] 2004-01-31 2004-02-29 2004-03-31 2004-04-30 2004-05-31 Dave -- Dr. David Forrest [EMAIL PROTECTED](804)684-7900w [EMAIL PROTECTED] (804)642-0662h http://maplepark.com/~drf5n/ __ R-help@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
Re: [R] adding 1 month to a date
Thanks. How do I se thsi 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 [EMAIL PROTECTED] 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 -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@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@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
Re: [R] adding 1 month to a date
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 [EMAIL PROTECTED] 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 -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@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@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
Re: [R] adding 1 month to a date
The chron package will do that: library(chron) seq(chron(01/31/00), by = months, len = 2)[2] [1] 02/29/00 On 10/12/05, Jim Porzak [EMAIL PROTECTED] wrote: OTOH, seq(as.Date(2004-01-31), by = month, length = 14) [1] 2004-01-31 2004-03-02 2004-03-31 2004-05-01 2004-05-31 [6] 2004-07-01 2004-07-31 2004-08-31 2004-10-01 2004-10-31 [11] 2004-12-01 2004-12-31 2005-01-31 2005-03-03 I would prefer to see dates forced to be within each month, not leaking into next month. IOW: [1] 2004-01-31 2004-02-29 2004-03-31 2004-04-30 2004-05-31, etc -- Jim Porzak Loyalty Matrix Inc. San Francisco, CA On 10/12/05, Marc Schwartz [EMAIL PROTECTED] wrote: Thanks to Prof. Ripley for pointing this out. One of the approaches that I had considered here was to set up a vector of the number of days in each month (adjusting of course for leap years), and use day arithmetic to add/subtract the appropriate number of days. However, it was easier to use seq.Date() and to further consider putting a wrapper around it to make it yet even easier to use. Marc On Wed, 2005-10-12 at 13:23 +0100, Prof Brian Ripley wrote: On Wed, 12 Oct 2005, bogdan romocea wrote: Simple addition and subtraction works as well: as.Date(1995/12/01,format=%Y/%m/%d) + 30 If you have datetime values you can use strptime(1995-12-01 08:00:00,format=%Y-%m-%d %H:%M:%S) + 30*24*3600 where 30*24*3600 = 30 days expressed in seconds. Sorry, not in general, as a month is not generally of 30 days (including in your example). seq.Date is a good way to do this. -Original Message- From: Marc Schwartz [mailto:[EMAIL PROTECTED] Sent: Tuesday, October 11, 2005 10:16 PM To: t c Cc: r-help@stat.math.ethz.ch Subject: Re: [R] adding 1 month to a date On Tue, 2005-10-11 at 16:26 -0700, t c wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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. There might be an easier way to do this, but using seq.Date(), you can increment or decrement from a Time 0 by months: Add 1 month: This takes your Time 0, generates a 2 element sequence (which begins with Time 0) and then takes the second element: seq(as.Date(1995-12-01), by = month, length = 2)[2] [1] 1996-01-01 Subtract 1 month: Same as above, but we use 'by = -1 month' and take the second element: seq(as.Date(1995-12-01), by = -1 month, length = 2)[2] [1] 1995-11-01 See ?as.Date and ?seq.Date for more information. The former function is used to convert from a character vector to a Date class object. Note that in your case, the date format is consistent with the default. Pay attention to the 'format' argument in as.Date() if your dates should be in other formats. HTH, Marc Schwartz __ R-help@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 __ R-help@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 __ R-help@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
Re: [R] adding 1 month to a date
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 [EMAIL PROTECTED] wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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@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 __ R-help@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
Re: [R] adding 1 month to a date
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 [EMAIL PROTECTED] 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 [EMAIL PROTECTED] 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 -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@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@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 __ R-help@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
Re: [R] adding 1 month to a date
On Tue, 2005-10-11 at 16:26 -0700, t c wrote: Within an R dataset, I have a date field called date_. (The dates are in the format -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. There might be an easier way to do this, but using seq.Date(), you can increment or decrement from a Time 0 by months: Add 1 month: This takes your Time 0, generates a 2 element sequence (which begins with Time 0) and then takes the second element: seq(as.Date(1995-12-01), by = month, length = 2)[2] [1] 1996-01-01 Subtract 1 month: Same as above, but we use 'by = -1 month' and take the second element: seq(as.Date(1995-12-01), by = -1 month, length = 2)[2] [1] 1995-11-01 See ?as.Date and ?seq.Date for more information. The former function is used to convert from a character vector to a Date class object. Note that in your case, the date format is consistent with the default. Pay attention to the 'format' argument in as.Date() if your dates should be in other formats. HTH, Marc Schwartz __ R-help@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