On Tue, Nov 30, 2010 at 9:56 PM, Luis Felipe Parra
<felipe.pa...@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

______________________________________________
R-help@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.

Reply via email to