Hi I need to add an year to and date field in the dataframe. Please help me X Date 1 2008-01-01 2 2008-02-01 3 2003-03-01 Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Adding-a-year-to-existing-date-tp4078930p4078930.html Sent from the R help mailing list archive at Nabble.com.
Adding a year to existing date
5 messages · arunkumar1111, Rolf Turner, MacQueen, Don +2 more
On 17/11/11 17:33, arunkumar1111 wrote:
Hi I need to add an year to and date field in the dataframe. Please help me X Date 1 2008-01-01 2 2008-02-01 3 2003-03-01
I can't find anything built in. This is probably because "year" is an
ill-defined
unit; years vary in length in a somewhat peculiar fashion. So doing
arithmetic
with respect to years is frowned on.
However you might try this:
`%+%` <- function(x,y){
if(!isTRUE(all.equal(y,round(y)))) stop("Argument \"y\" must be an
integer.\n")
x <- as.POSIXlt(x)
x$year <- x$year+y
as.Date(x)
}
Then:
xxx <- as.Date(c("2008-01-01","2008-02-01","2003-03-01"))
xxx %+% 1
[1] "2009-01-01" "2009-02-01" "2004-03-01"
Dunno what dangers lurk; caveat utilitor.
cheers,
Rolf Turner
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)
dates
[1] "2008-01-01" "2009-03-02"
dates2
[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
Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 11/16/11 8:33 PM, "arunkumar1111" <akpbond007 at gmail.com> wrote: >Hi > > I need to add an year to and date field in the dataframe. > >Please help me > >X Date >1 2008-01-01 >2 2008-02-01 >3 2003-03-01 > > >Thanks in advance > >-- >View this message in context: >http://r.789695.n4.nabble.com/Adding-a-year-to-existing-date-tp4078930p407 >8930.html >Sent from the R help mailing list archive at Nabble.com. > >______________________________________________ >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.
Just looking at the ambiguity in "adding a year"
dates <- as.Date(c('2007-03-01','2008-02-29'))
tmp <- as.POSIXlt(dates)
tmp$year <- tmp$year+1
dates2 <- as.Date(tmp)
dates2
[1] "2008-03-01" "2009-03-01"
dates2 - dates
Time differences in days [1] 366 366 KJ "MacQueen, Don" <macqueen1 at llnl.gov> wrote in message news:CAEA785F.7CFDB%macqueen1 at llnl.gov...
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)
dates
[1] "2008-01-01" "2009-03-02"
dates2
[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
--
Don MacQueen
Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550
925-423-1062
On 11/16/11 8:33 PM, "arunkumar1111" <akpbond007 at gmail.com> wrote:
Hi I need to add an year to and date field in the dataframe. Please help me X Date 1 2008-01-01 2 2008-02-01 3 2003-03-01 Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/Adding-a-year-to-existing-date-tp4078930p407 8930.html Sent from the R help mailing list archive at Nabble.com.
______________________________________________ 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.
Hi
On 17/11/11 17:33, arunkumar1111 wrote:
Hi I need to add an year to and date field in the dataframe. Please help me X Date 1 2008-01-01 2 2008-02-01 3 2003-03-01
I can't find anything built in. This is probably because "year" is an
ill-defined
unit; years vary in length in a somewhat peculiar fashion. So doing
arithmetic
with respect to years is frowned on.
However you might try this:
`%+%` <- function(x,y){
if(!isTRUE(all.equal(y,round(y)))) stop("Argument \"y\" must be an
integer.\n")
x <- as.POSIXlt(x)
x$year <- x$year+y
as.Date(x)
}
Then:
xxx <- as.Date(c("2008-01-01","2008-02-01","2003-03-01"))
xxx %+% 1
[1] "2009-01-01" "2009-02-01" "2004-03-01"
another option is paste((as.numeric(format(xxx, "%Y"))+1), format(xxx,"%m-%d"), sep="-") Regards Petr
Dunno what dangers lurk; caveat utilitor.
cheers,
Rolf Turner
______________________________________________ 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.