简体   繁体   中英

Can you round a date to either the 1st or 15th based on they day of the month using r?

In the below piece of code I generate a date based off:

  1. grabbing the first date from a character string that has one or more dates
  2. adding a numeric value (lead time) to that date to get a new date

What I am trying to do is to now make the resulting date either the 15th of the month or the 1st of the month. This would be done by saying "if the day of the month is 15th or greater, then make the date the 15th of said month, if it is less than 15 then make the date the 1st of said month")

I am much more familiar with SQL but not quite sure how do to this the most simple way in R

Code:

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

sample data:

                 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

We can use 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"

Or with case_when

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

Or using base R

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

Or another option is gsubfn to change from a character string

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"

data

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


  

You could get the day, compare and assign value based on it.

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]

This is another way of writing

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM