您可以使用 r 将日期四舍五入到 1 号或 15 号吗?
Can you round a date to either the 1st or 15th based on they day of the month using r?
在下面的代码中,我生成了一个基于关闭的日期:
- 从具有一个或多个日期的字符串中抓取第一个日期
- 向该日期添加数值(提前期)以获得新日期
我现在要做的是将结果日期设置为该月的 15 日或该月的 1 日。这可以通过说 "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")
来完成
我对 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
您可以获取日期,并据此进行比较和赋值。
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"
我们可以使用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'))
在下面的代码中,我生成了一个基于关闭的日期:
- 从具有一个或多个日期的字符串中抓取第一个日期
- 向该日期添加数值(提前期)以获得新日期
我现在要做的是将结果日期设置为该月的 15 日或该月的 1 日。这可以通过说 "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")
来完成我对 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
您可以获取日期,并据此进行比较和赋值。
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"
我们可以使用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'))