[R] Changing the day of the month in a date
Gabor Grothendieck
ggrothendieck at gmail.com
Wed Dec 1 05:57:39 CET 2010
On Tue, Nov 30, 2010 at 9:56 PM, Luis Felipe Parra
<felipe.parra at quantil.com.co> wrote:
> Hello, I want to change the day of the month in a date object. What I am
> doing at the moment is:
>
> x=as.POSIXlt(x)
> x$mday=13
> x=as.Date(x)
>
> Does anybody know if there is a more "natural" (eficient) way to do this
Here are a couple of ways to change the date to the 13th of the month.
The first way converts d to character and replaces the last two
characters with the new day and then converts back to Date. The
second converts to a "yearmon" object which when converted back to
"Date" becomes the first of the month. Adding thirteen to that minus
1 gives the 13th of the month.
# test data
d <- as.Date("2001-04-15")
# 1
as.Date(sub("..$", 13, d))
# 2
library(zoo)
as.Date(as.yearmon(d)) + 13 - 1
--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com
More information about the R-help
mailing list