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? Thank you! -- Dimitri Liakhovitski Ninah Consulting www.ninah.com
You've tried: as.numeric(format(Sys.Date(), '%Y')) On Fri, Aug 13, 2010 at 4:36 PM, Dimitri Liakhovitski < dimitri.liakhovitski@gmail.com> 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? > > Thank you! > > > -- > Dimitri Liakhovitski > Ninah Consulting > www.ninah.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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
myFrame$year<-years(strptime(x)) On Fri, Aug 13, 2010 at 12:36 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> 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? > > Thank you! > > > -- > Dimitri Liakhovitski > Ninah Consulting > www.ninah.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. >
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
Possibly Parallel Threads
- finding the year of a date
- Creating a "shifted" month (one that starts not on the first of each month but on another date)
- summing values by week - based on daily dates - but with some dates missing
- Extracting months and years from Dates while keeping order
- transforming a badly organized data base into a list of data frames