我想排除今天之前和今天2周之后的数据。我目前有这样的数据。 例如今天是01/09/19
promo date item discount stock stock/day
a 30/08/19 ax 15% 200 50
a 31/08/19 ax 15% 200 50
a 01/09/19 ax 15% 200 50
a 02/09/19 ax 15% 200 50
a 30/08/19 bx 15% 200 50
a 31/08/19 bx 15% 200 50
a 01/09/19 bx 15% 200 50
a 02/09/19 bx 15% 200 50
a 27/08/19 cx 25% 200 200
a 27/08/19 dx 15% 200 200
b 03/09/19 ex 15% 200 25
b 04/09/19 ex 15% 200 25
b 09/09/19 ex 15% 200 25
b 10/09/19 ex 15% 200 25
b 15/09/19 fx 15% 200 200
b 16/09/19 fx 15% 200 200
b 21/09/19 fx 15% 200 200
b 22/09/19 fx 15% 200 200
b 25/09/19 fx 15% 200 200
因此数据将被排除,最终数据将如下所示。
promo date item discount stock stock/day
a 01/09/19 ax 15% 200 50
a 02/09/19 ax 15% 200 50
a 01/09/19 bx 15% 200 50
a 02/09/19 bx 15% 200 50
b 03/09/19 ex 15% 200 25
b 04/09/19 ex 15% 200 25
b 09/09/19 ex 15% 200 25
b 10/09/19 ex 15% 200 25
该怎么做?提前非常感谢
答案 0 :(得分:3)
首先将date
转换为标准格式,然后仅保留那些在范围内的行
todays_date <- as.Date("01/09/19", "%d/%m/%y")
df$date1 <- as.Date(df$date, "%d/%m/%y")
df[df$date1 >= todays_date & df$date1 < (todays_date + 14), ]
# promo date item discount stock stock.day date1
#3 a 01/09/19 ax 15% 200 50 2019-09-01
#4 a 02/09/19 ax 15% 200 50 2019-09-02
#7 a 01/09/19 bx 15% 200 50 2019-09-01
#8 a 02/09/19 bx 15% 200 50 2019-09-02
#11 b 03/09/19 ex 15% 200 25 2019-09-03
#12 b 04/09/19 ex 15% 200 25 2019-09-04
#13 b 09/09/19 ex 15% 200 25 2019-09-09
#14 b 10/09/19 ex 15% 200 25 2019-09-10
如果以后不需要,可以删除date1
列。
如果您更喜欢链条和包裹,我们也可以
library(dplyr)
library(lubridate)
df %>%
mutate(date1 = dmy(date)) %>%
filter(date1 >= todays_date & date1 < (todays_date + 14))