创建分隔列,其中包含两个其他列中日期之间的所有日期

时间:2015-01-08 17:45:28

标签: r

我有一个数据框,显示零售商的产品ID,以及每个产品可供销售的日期以及销售何时结束。

所以:

Retailer   Product   Active        Closed
123        A1        2014-01-01    2014-07-28  
135        A1        2014-03-05    2015-06-02
135        A2        2014-02-28    2014-03-25

我想创建一个表格,每天告诉我每个产品在每个零售商处出售,无论是广泛格式还是长格式。

所以,要么:

Retailer   Product   Active        Closed       ForSale
123        A1        2014-01-01    2014-07-28   2014-01-01; 2014-01-02; etc 
135        A1        2014-03-05    2015-06-02   2014-03-05; 2014-03-06; etc
135        A2        2014-02-28    2014-03-25   2014-02-28; 2014-03-01; etc

(显然没有等...列出每一个日期)

或者:

Retailer    Product    ForSale
123         A1         2014-01-01
123         A1         2014-01-02
123         A1         2014-01-03

...一直到列出所有内容。

我试过使用seq,如下:

myData$ForSale <- seq(as.Date(myData$Active), as.Date(myData$Closed), by = "days")

但它告诉我'from'必须是1的长度,我并没有真正遵循。

有什么想法吗?谢谢!

仅供参考,str(数据):

Classes ‘data.table’ and 'data.frame':  239440 obs. of  7 variables:
 $ iGame    : int  833 833 833 833 833 833 833 833 892 892 ...
 $ Pack     : int  180899 183796 188712 188716 178789 178792 178800 186807 14589 14639 ...
 $ ToUKey   : int  1002 1002 1002 1002 1002 1002 1002 1002 1002 1002 ...
 $ Settled  : Date, format: "2011-07-26" "2011-07-26" "2011-07-26" "2011-07-26" ...
 $ Received1: chr  "" "" "2011-07-06" "2011-07-06" ...
 $ Active   : Date, format: "2011-07-10" "2011-07-07" "2011-07-12" "2011-07-13" ...
 $ Received2: chr  "" "" "2011-07-06" "2011-07-06" ...
 - attr(*, ".internal.selfref")=<externalptr> 

1 个答案:

答案 0 :(得分:3)

您可以尝试data.table。以下代码将以“长”格式获得结果。

library(data.table)
setDT(df)[ ,list(ForSale=seq(Active, Closed, by='1 day')), 
                                by=list(Product, Retailer)]

或者您可以创建list列“ForSale”

setDT(df)[ ,list(ForSale=list(seq(Active, Closed, by='1 day'))),
                              by=list(Product, Retailer)]

数据

df <- structure(list(Retailer = c(123L, 135L, 135L), Product = c("A1", 
 "A1", "A2"), Active = structure(c(16071, 16134, 16129), class = "Date"), 
Closed = structure(c(16279, 16588, 16154), class = "Date")), 
.Names = c("Retailer", "Product", "Active", "Closed"), row.names = c(NA, -3L),
class = "data.frame")