I have the following dataframe:
Data <- data.frame(
date = c(\"2001-01-01\", \"2001-02-01\", \"2001-03-01\", \"2001-04-01\", \"2001-05-01\", \"2001-06-0
another option would be:
Data$qtr <- lubridate::quarter(Data$date, with_year = T)
The data.table
package and its IDate
class have some nice convenience functions (e.g. quarter()
, year()
), similar to those of lubridate
available. paste0()
them together as you please.
Data <- data.frame(
date = c("2001-01-01", "2001-02-01", "2001-03-01",
"2001-04-01", "2001-05-01", "2001-06-01")
)
require(data.table)
setDT(Data)
Data[ , date := as.IDate(date) ] # data.table s integer based date class
Data[ , qtr := paste0(year(date), '/', quarter(date)) ]
# your specific format
Data[ , qt2 := paste0(substr(year(date),3,4),
'/',
paste0('0', quarter(date))) ]