[R] transforming dates into years

David Winsemius dwinsemius at comcast.net
Fri Aug 13 21:41:22 CEST 2010


On Aug 13, 2010, at 3:36 PM, Dimitri Liakhovitski wrote:

> Hello!
>
> If I have in my data frame MyFrame a variable saved as a Date and want
> to translate it into years, I currently do it like this using "zoo":
>
> library(zoo)
> as.year <- function(x) as.numeric(floor(as.yearmon(x)))
> myFrame$year<-as.year(myFrame$date)
>
> Is there a function that would do it directly - like "as.yearmon" -
> but for years?

I suspect most would just use format:

?format
 > dd <- as.Date("2010-01-01")
 > format(dd, "%Y")
[1] "2010"


-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list