我需要根据日期周期填充数据并更改列的格式,因此我的数据可以由商业智能程序Qlik Sense读取。所以我下面有文件名为“ axn”的数据。然后,该文件名将被填充到名为“ store”的新列中。
promo start date end date item discount stock
a 30/08/19 02/09/19 ax 15% 200
a 30/08/19 02/09/19 bx 15% 200
a 27/08/19 27/08/19 cx 25% 200
a 27/08/19 27/08/19 dx 15% 200
b 03/09/19 10/09/19 ex 15% 200
b 04/09/19 04/09/19 fx 15% 200
要这样:
store promo date item discount stock
axn a 30/08/19 ax 15% 200
axn a 31/08/19 ax 15% 200
axn a 01/09/19 ax 15% 200
axn a 02/09/19 ax 15% 200
axn a 30/08/19 bx 15% 200
axn a 31/08/19 bx 15% 200
axn a 01/09/19 bx 15% 200
axn a 02/09/19 bx 15% 200
axn a 27/08/19 cx 25% 200
axn a 27/08/19 dx 15% 200
axn b 03/09/19 ex 15% 200
axn b 04/09/19 ex 15% 200
... continue as above
axn b 09/09/19 ex 15% 200
axn b 10/09/19 ex 15% 200
axn b 04/09/19 fx 15% 200
有人知道该怎么做吗?提前谢谢
答案 0 :(得分:2)
您可以先使用tidyverse
将startdate
和enddate
转换为实际日期,然后使用seq
在它们之间创建一个顺序,并使用文件名创建一个新列。
library(tidyverse)
df %>%
mutate_at(vars(startdate, enddate), as.Date, format = "%d/%m/%y") %>%
unnest(date = map2(startdate, enddate, seq, by = "day")) %>%
mutate(store = 'axn')
# promo startdate enddate item discount stock date store
#1 a 2019-08-30 2019-09-02 ax 15% 200 2019-08-30 axn
#2 a 2019-08-30 2019-09-02 ax 15% 200 2019-08-31 axn
#3 a 2019-08-30 2019-09-02 ax 15% 200 2019-09-01 axn
#4 a 2019-08-30 2019-09-02 ax 15% 200 2019-09-02 axn
#5 a 2019-08-30 2019-09-02 bx 15% 200 2019-08-30 axn
#6 a 2019-08-30 2019-09-02 bx 15% 200 2019-08-31 axn
#7 a 2019-08-30 2019-09-02 bx 15% 200 2019-09-01 axn
#8 a 2019-08-30 2019-09-02 bx 15% 200 2019-09-02 axn
#9 a 2019-08-27 2019-08-27 cx 25% 200 2019-08-27 axn
#10 a 2019-08-27 2019-08-27 dx 15% 200 2019-08-27 axn
#11 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-03 axn
#12 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-04 axn
#13 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-05 axn
#14 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-06 axn
#15 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-07 axn
#16 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-08 axn
#17 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-09 axn
#18 b 2019-09-03 2019-09-10 ex 15% 200 2019-09-10 axn
#19 b 2019-09-04 2019-09-04 fx 15% 200 2019-09-04 axn
数据
df <- structure(list(promo = structure(c(1L, 1L, 1L, 1L, 2L, 2L),
.Label = c("a", "b"), class = "factor"), startdate = structure(c(4L, 4L, 3L,
3L, 1L, 2L), .Label = c("03/09/19", "04/09/19", "27/08/19", "30/08/19"
), class = "factor"), enddate = structure(c(1L, 1L, 4L, 4L, 3L,
2L), .Label = c("02/09/19", "04/09/19", "10/09/19", "27/08/19"
), class = "factor"), item = structure(1:6, .Label = c("ax",
"bx", "cx", "dx", "ex", "fx"), class = "factor"), discount =
structure(c(1L, 1L, 2L, 1L, 1L, 1L), .Label = c("15%", "25%"), class = "factor"),
stock = c(200L, 200L, 200L, 200L, 200L, 200L)), class = "data.frame",
row.names = c(NA, -6L))