Format date as Year/Quarter

前端 未结 8 1358
隐瞒了意图╮
隐瞒了意图╮ 2020-12-03 14:06

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         


        
相关标签:
8条回答
  • 2020-12-03 14:29

    another option would be:

    Data$qtr <- lubridate::quarter(Data$date, with_year = T)
    
    0 讨论(0)
  • 2020-12-03 14:34

    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))) ]
    
    0 讨论(0)
提交回复
热议问题