The data.table
package introduced the IDate
class some time ago and zoo
-package-like functions to retrieve months, days, etc (Check ?IDate
). so, you can extract the desired info now in the following ways:
require(data.table)
df <- data.frame(id = 1:3,
date = c("2004-02-06" , "2006-03-14" , "2007-07-16"))
setDT(df)
df[ , date := as.IDate(date) ] # instead of as.Date()
df[ , yrmn := paste0(year(date), '-', month(date)) ]
df[ , yrmn2 := format(date, '%Y-%m') ]