【问题标题】:Can you round a date to either the 1st or 15th based on they day of the month using r?您可以使用 r 将日期四舍五入到 1 日或 15 日吗?
【发布时间】:2020-02-15 04:55:34
【问题描述】:

在下面的代码中,我根据以下代码生成了一个日期:

  1. 从具有一个或多个日期的字符串中获取第一个日期
  2. 将数值(提前期)添加到该日期以获得新日期

我现在要做的是使结果日期为每月 15 日或每月 1 日。这可以通过说“如果该月的日期为 15 日或更大,则将日期设为该月的 15 日,如果小于 15,则将日期设为该月的 1 日”)

我对 SQL 更熟悉,但不太清楚如何在 R 中使用最简单的方法来实现这一点

代码:

AAR_Combined_w_LL$newvalue <-mdy(substr(AAR_Combined_w_LL$order_cutoffs,0,10)) + AAR_Combined_w_LL$lead_time

样本数据:

                 season article_number                    order_cutoffs amount_of_limited_order_cut_offs retail_intro_date lead_time
1 adidas Fall/Winter 2020         GI7954 12/17/2019,01/14/2020,02/25/2020                                3        2020-07-01       105
2 adidas Fall/Winter 2020         GI7955 12/17/2019,01/14/2020,02/25/2020                                3        2020-07-01       105
3 adidas Fall/Winter 2020         P82146                       12/17/2019                                1        2020-06-01        75
4 adidas Fall/Winter 2020         S86676                       12/17/2019                                1        2020-06-01        75
5 adidas Fall/Winter 2020         P82145                       12/17/2019                                1        2020-06-01        75
6 adidas Fall/Winter 2020         S86673                       12/17/2019                                1        2020-06-01        75
  1st_Booking_Deadline   1st_BW_Retail_Windows 2nd_Booking_Deadline   2nd_BW_Retail_Windows 3rd_Booking_Deadline 3rd_BW_Retail_Windows
1           2019-12-06 2020-07-01 - 2020-08-01           2020-01-24 2020-09-01 - 2020-11-01                                           
2           2019-12-06 2020-07-01 - 2020-08-01           2020-01-24 2020-09-01 - 2020-11-01                                           
3           2019-12-06 2020-06-01 - 2020-11-01                                                                                        
4           2019-12-06 2020-06-01 - 2020-11-01                                                                                        
5           2019-12-06 2020-06-01 - 2020-11-01                                                                                        
6           2019-12-06 2020-06-01 - 2020-11-01                                                                                        
  4th_Booking_Deadline 4th_BW_Retail_Windows 5th_Booking_Deadline 5th_BW_Retail_Windows            Booking_Deadlines Booking_Deadline_Intervals
1                                                                                       2019-12-06, 2020-01-24, , ,                           2
2                                                                                       2019-12-06, 2020-01-24, , ,                           2
3                                                                                                 2019-12-06, , , ,                        NULL
4                                                                                                 2019-12-06, , , ,                        NULL
5                                                                                                 2019-12-06, , , ,                        NULL
6                                                                                                 2019-12-06, , , ,                        NULL
    newvalue
1 2020-03-31
2 2020-03-31
3 2020-03-01
4 2020-03-01
5 2020-03-01
6 2020-03-01

【问题讨论】:

    标签: r date lubridate


    【解决方案1】:

    我们可以使用ifelse

    library(lubridate)
    day(x) <- ifelse(day(x) > 15, 15, 1)
    x
    #[1] "2020-03-15" "2020-03-15" "2020-03-01" "2020-03-01" "2020-03-01"
    

    case_when

    day(x) <- case_when(day(x) > 15 ~ 15, TRUE ~ 1)
    

    或使用base R

    day(x) <- ifelse(as.integer(format(x, "%d")) > 15, 15, 1)
    

    或者另一个选项是gsubfn 从字符串更改

    library(gsubfn)
    as.Date(gsubfn("(\\d+)$", ~ ifelse(as.numeric(x) > 15, 15, 1), as.character(x)))
    #[1] "2020-03-15" "2020-03-15" "2020-03-01" "2020-03-01" "2020-03-01"
    

    数据

    x <- as.Date(c('2020-03-31','2020-03-31','2020-03-01','2020-03-01','2020-03-01'))
    
    
      
    

    【讨论】:

      【解决方案2】:

      您可以获取日期,根据它进行比较和分配价值。

      library(lubridate)
      x <- as.Date(c('2020-03-31','2020-03-31','2020-03-01','2020-03-01','2020-03-01'))
      
      day(x) <- c(1, 15)[(day(x) >= 15) + 1]
      

      这是另一种写法

      day(x) <- ifelse(day(x) >= 15, 15, 1)
      x
      #[1] "2020-03-15" "2020-03-15" "2020-03-01" "2020-03-01" "2020-03-01"
      

      【讨论】:

        猜你喜欢
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多