【问题标题】:Get last day of month from YYYYMM (year/month) variable in R [duplicate]从R中的YYYYMM(年/月)变量获取一个月的最后一天[重复]
【发布时间】:2021-12-04 17:14:10
【问题描述】:

我想从格式为整数 YYYYMM 的年/月变量中获取该月的最后一天。

yearmonth <- c(seq(202001,202012), 
            seq(202101,202112))

我正在寻找的输出如下。例如,2020 年 2 月的最后一天是 29 日(2020 年是闰年),而 2021 年 2 月的最后一天是 28 日。

last <- c(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 
          31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)

理想情况下,我想使用lubridate 包。

【问题讨论】:

    标签: r date


    【解决方案1】:

    使用zooas.yearmon -

    yearmonth |> 
      as.character() |>
      zoo::as.yearmon('%Y%m') |>
      as.Date(frac = 1) |>
      format('%d')
    
    #[1] "31" "29" "31" "30" "31" "30" "31" "31" "30" "31" "30" "31" "31" "28"
    #[15] "31" "30" "31" "30" "31" "31" "30" "31" "30" "31"
    

    使用lubridateceiling_date函数。

    library(lubridate)
    format(ceiling_date(ymd(paste0(yearmonth, '01')), 'month') - 1, '%d')
    

    【讨论】:

    • 非常感谢!你知道如何使用lubricate 包吗?
    • 更新了答案以使用lubridate
    【解决方案2】:

    使用 lubridate,我在每个提供的月份的第一天加上一个月,然后减去一天。该日期应该是您月份的最后一天。

    yearmonth <- c(seq(202001,202012), 
                   seq(202101,202112))
    yearmonthday <- as.Date(paste0(yearmonth, "01"), format = "%Y%m%d")
    
    library(lubridate)
    last <- as.numeric(format(yearmonthday + months(1) - days(1), format = "%d"))
    last
    

    【讨论】:

      【解决方案3】:

      更新:

      请注意 akrun 的评论,我们可以使用 th ymd() 函数的truncated 参数来声明可以截断的格式数量:

      days_in_month(ymd(yearmonth, truncated = 1))

      第一个答案:

      这是lubridate 解决方案:

      1. 构造年月日等日期元素
      2. 使用make_date() 获取日期类
      3. 然后使用 lubridate 中的days_in_month() 函数
      library(lubridate)
      
      my_year <- substr(yearmonth,1,4)
      my_month <- as.integer(substr(yearmonth,5,6))
      my_day <- rep(1, length(my_year))
      
      
      days_in_month(make_date(my_year, my_month, my_day))
      
      # you can wrape around `unname` to get vector without names
      

      输出:

      Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul 
       31  29  31  30  31  30  31  31  30  31  30  31  31  28  31  30  31  30  31 
      Aug Sep Oct Nov Dec 
       31  30  31  30  31
      
      # without names:
      
      unname(days_in_month(make_date(my_year, my_month, my_day)))
      [1] 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31
      

      【讨论】:

      • 很好地利用了一个月中的天数。您可以使用 days_in_month(ymd(yearmonth, truncated = 1)) 使其紧凑
      • @akrun。谢谢你,这是对我知识的重要指导!
      【解决方案4】:

      使用 lurbidate:

      library(lubridate)
      
      day(ceiling_date(as.Date(paste0(yearmonth,'01'), format = '%Y%m%d'), unit = 'month') - 1)
       [1] 31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31
      

      【讨论】:

        【解决方案5】:

        基础 R:

        尝试使用:

        day(as.Date(cut(as.Date(paste0(yearmonth, "01"), format = "%Y%m%d") + 32, 'month')) - 1)
        

        输出:

        31 29 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30 31 31 30 31 30 31
        

        解释:

        这会将32 天添加到所有日期,并使用cut 函数将其减少month(获取每个月的第一天)。之后,它会从日期中减去1,这将给出原始月份的最后一天

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-11-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-25
          相关资源
          最近更新 更多