Skip to content
Prev 277741 / 398506 Next

Adding a year to existing date

Here is an example that could probably be described as "adding a year":

dates <- c('2008-01-01','2009-03-02')
tmp <- as.POSIXlt(dates)tmp$year <- tmp$year+1
dates2 <- format(tmp)
[1] "2008-01-01" "2009-03-02"
[1] "2009-01-01" "2010-03-02"

## to begin to understand how it works, give the command
##   unclass(tmp)
## (and read the help pages
##   ?as.POSIXlt
##   ?DateTimeClasses

Another example:

dates <- as.Date(c('2008-01-01','2009-03-02'))
tmp <- as.POSIXlt(dates)
tmp$year <- tmp$year+1
dates2 <- as.Date(tmp)



##   ?as.Date
##   ?Date




-Don