Create end of the month date from a date variable

前端 未结 8 541
抹茶落季
抹茶落季 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:11

    A function as below would do the work (assume dt is scalar) -

    month_end <- function(dt) {
        d <- seq(dt, dt+31, by="days")
        max(d[format(d,"%m")==format(dt,"%m")])
    }
    

    If you have a vector of Dates, then do the following -

    sapply(dates, month_end)
    

提交回复
热议问题