Adding to the years ...
There is some question of what should be returned if its Feb 29th and in 6 years Feb only has 28 days; however, that aside try this which does not involve any internals and works for both Date and chron dates:
y6 <- function(x) {
+ for(i in seq_along(x)) x[i] <- tail(seq(x[i], len = 7, by = "year"), 1) + x + }
x <- as.Date("2000-02-28") + 0:2
y6(x)
[1] "2006-02-28" "2006-03-01" "2006-03-01"
library(chron)
x <- as.chron("2000-02-28") + 0:2
y6(x)
[1] 02/28/06 02/28/06 03/01/06
On Mon, Nov 23, 2009 at 9:06 PM, Rolf Turner <r.turner at auckland.ac.nz> wrote:
I have a character vector of dates, comme ci:
?> irving
?[1] "12/05/1987" "11/07/1986" "08/29/1987" "01/26/1988" "06/21/1988"
?[6] "03/17/1987" "02/24/1988" "10/04/1986" "03/19/1987" "01/02/1987"
For reasons best known to myself ( ;-) ) I want to add six years to the
year in each of these dates.
After reading TFM about POSIXct and POSIXlt objects I figured out that I
could do:
? ? ? ?clyde <- as.POSIXlt(irving,format="%m/%d/%Y")
? ? ? ?clyde$year <- clyde$year + 6
? ? ? ?melvin <- format(clyde,"%m/%d/%Y")
This seems to me however to be a bit of a kludge. ?Is there a way of
converting a character vector of dates to a POSIXlt/POSIXct/Date/whatever
object and then adding 6 years to the latter object in a straightforward
way, without having to delve into components of the object?
Ta.
? ? ? ?cheers,
? ? ? ? ? ? ? ?Rolf Turner
######################################################################
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
______________________________________________ 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.