Create end of the month date from a date variable

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

    We can also use bsts::LastDayInMonth:

    transform(df, date.end.month = bsts::LastDayInMonth(df$date.start.month))
    
    #   date.start.month date.end.month
    # 1       2012-01-01     2012-01-31
    # 2       2012-02-01     2012-02-29
    # 3       2012-03-01     2012-03-31
    # 4       2012-04-01     2012-04-30
    
    0 讨论(0)
  • 2020-12-03 07:02

    A straightforward solution would be using the yearmonfunction with the argument frac=1 from the xts-package. frac is a number between 0 and 1 that indicates the fraction of the way through the period that the result represents.

    as.Date(as.yearmon(seq.Date(as.Date('2017-02-01'),by='month',length.out = 6)),frac=1)
    
    [1] "2017-02-28" "2017-03-31" "2017-04-30" "2017-05-31" "2017-06-30" "2017-07-31"
    

    Or if you prefer “piping” using magrittr:

    seq.Date(as.Date('2017-02-01'),by='month',length.out = 6) %>%
             as.yearmon() %>% as.Date(,frac=1)
    
    [1] "2017-02-28" "2017-03-31" "2017-04-30" "2017-05-31" "2017-06-30" "2017-07-31"
    
    0 讨论(0)
  • 2020-12-03 07:04

    you can use timeperiodsR

    date.start.month=seq(as.Date("2012-01-01"),length=4,by="months")
    df=data.frame(date.start.month)
    df$date.start.month
    
    # install.packages("timeperiodsR") 
    
    pm <- previous_month(df$date.start.month[1]) # get previous month
    
    start(pm) # first day of previous month
    end(pm)   # last day of previous month
    seq(pm)   # vector with all days of previous month
    
    0 讨论(0)
  • 2020-12-03 07:10

    Here is another solution using the lubridate package:

    date.start.month=seq(as.Date("2012-01-01"),length=4,by="months")
    df=data.frame(date.start.month)
    
    library(lubridate)
    df$date.end.month <- ceiling_date(df$date.start.month, "month") - days(1)
    df$date.end.month
    [1] "2012-01-31" "2012-02-29" "2012-03-31" "2012-04-30"
    

    This uses the same concept given by James above, in that it gets the first day of the next month and subtracts one day.

    By the way, this will work even when the input date is not necessarily the first day of the month. So for example, today is the 27th of the month and it still returns the correct last day of the month:

    ceiling_date(Sys.Date(), "month") - days(1)
    [1] "2017-07-31"
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-03 07:22

    Use timeLastDayInMonth from the timeDate package:

    df$eom<-timeLastDayInMonth(df$somedate)
    
    0 讨论(0)
提交回复
热议问题