Extract only quarter from a date in r

后端 未结 4 1796
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 08:41

I would like to extract ONLY the quarter from a date, e.g., to get an integer 1 from the date \"2003-02-08\". I have been trying something along this line

li         


        
4条回答
  •  北恋
    北恋 (楼主)
    2021-01-18 09:21

    There is a base R function, quarters, that more or less accomplishes what you want, though it prepends "Q". So

    quarters(as.Date("2001-05-01"))
    [1] "Q2"
    

    If it is important to get rid of the "Q", you could use substr

    substr(quarters(as.Date("2001-05-01")), 2, 2)
    [1] "2"
    

    Other date-related base R functions, such as weekdays and months can be found in help page ?quarters.

提交回复
热议问题