Converting yearmon column to last date of the month in R

前端 未结 3 1527
天命终不由人
天命终不由人 2021-01-15 03:03

I have a data frame (df) like the following:

Date        Arrivals

2014-07      100
2014-08      150
2014-09      200

I know that I can con

3条回答
  •  佛祖请我去吃肉
    2021-01-15 03:32

    Using lubridate, you can add a month and subtract a day to get the last day of the month:

    library(lubridate)
    
    ymd(paste0(df$Date, '-01')) + months(1) - days(1)
    # [1] "2014-07-31" "2014-08-31" "2014-09-30"
    

提交回复
热议问题