Create end of the month date from a date variable

前端 未结 8 518
抹茶落季
抹茶落季 2020-12-03 06:27

I have a large data frame with date variables, which reflect first day of the month. Is there an easy way to create a new data frame date variable that represents the last d

相关标签:
8条回答
  • 2020-12-03 07:24

    To get the end of months you could just create a Date vector containing the 1st of all the subsequent months and subtract 1 day.

    date.end.month <- seq(as.Date("2012-02-01"),length=4,by="months")-1
    date.end.month
    [1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"
    
    0 讨论(0)
  • 2020-12-03 07:27
    library(lubridate)
     as.Date("2019-09-01") - days(1)
    [1] "2019-08-31"
    

    or

    library(lubridate)
     as.Date("2019-09-01") + months(1) - days(1)
    [1] "2019-09-30"
    
    0 讨论(0)
提交回复
热议问题